简化;吉特加上;“之后”;git diff“;

简化;吉特加上;“之后”;git diff“;,git,Git,在将任何文件添加到git之前,我总是经常调用git diff,并使用其他选项(例如“-w”、“-word diff”等) 一旦diff看起来不错,我就通过“gitadd”为提交准备文件。目前我这样做的方式是修改以前的命令行git diffbash调用,这需要删除diff和任何感觉低效和冗余的选项 如何通过命令行执行diff和可选的add操作?要有选择地应用diff块,请使用-paka--patch选项git add-p从工作树应用到索引,git checkout-p从索引或命名的提交应用到索引和

在将任何文件添加到git之前,我总是经常调用
git diff
,并使用其他选项(例如“
-w
”、“
-word diff
”等)

一旦
diff
看起来不错,我就通过“
gitadd
”为提交准备文件。目前我这样做的方式是修改以前的命令行
git diff
bash调用,这需要删除
diff
和任何感觉低效和冗余的选项


如何通过命令行执行
diff
和可选的
add
操作?

要有选择地应用diff块,请使用
-p
aka
--patch
选项
git add-p
从工作树应用到索引,
git checkout-p
从索引或命名的提交应用到索引和工作树,
git reset-p
从提交应用到索引

你对每个帅哥的选择是

你和谁在一起?或者只是按回车键


为了一般不在命令行中重复您自己的操作,
$\u
扩展到上一个命令的最后一个参数,例如
git diff--word diff@path/to/file
,然后只
git add$\u
。还有一个历史扩展,它从历史行中提取单词,而不是命令,并且根据shell略有不同,
$
是“上一行的最后一个字”,
#$
是“我现在键入的行中的最后一个单词”,因此就地排序是例如
sort-nrk2,2-o mylist#$

我认为您正在寻找交互模式,您可以通过
git add-I
(或
-p
)进入该模式。有很多IDE和git工具可以执行类似的工作流。但是
gitadd-p
可能会做你想做的事情:在添加和拒绝帅哥的同时查看差异。否则,您可能会编写一个定制的可执行文件来执行您想要的操作。在
路径中名为
git-foo
的任何可执行文件将作为
git-foo
提供;上面的建议都很好,但是假设zsh或bash(可能还有其他unixshell)的话,一个有用的提示是escape后跟一个“.”插入最后一个命令的最后一个参数。因此,您可以为
git diff
设置别名
gd
,为
git add
设置别名
ga
,然后它将是
gd
,后跟
ga。
@mikej您也可以按住Alt键,而不是按住ESC键。对于大多数其他ESC/Alt命令也是如此;ESC是不支持Alt的键盘的唯一兼容性选项。@mikej这可能是您想要的:
Apply this hunk to index [y,n,q,a,d,/,j,J,g,e,?]? 
y - apply this hunk to index
n - do not apply this hunk to index
q - quit; do not apply this hunk or any of the remaining ones
a - apply this hunk and all later hunks in the file
d - do not apply this hunk or any of the later hunks in the file
g - select a hunk to go to
/ - search for a hunk matching the given regex
j - leave this hunk undecided, see next undecided hunk
J - leave this hunk undecided, see next hunk
k - leave this hunk undecided, see previous undecided hunk
K - leave this hunk undecided, see previous hunk
s - split the current hunk into smaller hunks
e - manually edit the current hunk
? - print help