简化"git add"操作在"git diff"之后, [英] Simplify "git add" After "git diff"

查看:120
本文介绍了简化"git add"操作在"git diff"之后,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

一旦 diff 看起来不错,我将通过" git add< filename> "将文件提交.我目前的操作方式是修改以前的命令行 git diff bash调用,该操作需要删除 diff 和任何感觉效率低下的选项,并且是多余的方法.

如何通过命令行一起执行 diff 和可选的 add ?

解决方案

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

每个大块的选项是

 将这个块应用到索引[y,n,q,a,d,/,j,J,g,e ,?]?y-将大块应用于索引n-不要将此大块应用于索引q-退出;不要应用此大块或其余任何大块a-在文件中应用此大块以及所有以后的大块d-不要在文件中应用此大块或以后的任何大块g-选择一个大块头去/-搜索与给定正则表达式匹配的块j-尚未确定该大块,请参阅下一个未确定的大块J-尚未确定此大块,请参阅下一个大块k-尚未确定此大块,请参阅先前未确定的大块K-尚未确定此大块,请参阅先前的大块s-将当前块拆分为较小的块e-手动编辑当前大块?-打印帮助 

您得到了什么?或只是按Enter键.

通常只在命令行中不重复自己,所以 $ _ 会扩展到上一个命令的最后一个参数,例如 git diff --word-diff @ path/to/file ,然后只需 git add $ _ .还有一个历史扩展,它从历史行而不是命令中提取单词,并因外壳而略有不同,!$ 是上一行的最后一个单词",!#$ 是我现在输入的行中的最后一个单词",因此就地排序例如 sort -nrk2,2 -o mylist!#$ .

Before adding any file to git, I always call git diff <filename> often with additional options (e.g., "-w", "--word-diff", etc.).

Once the diff looks good, I stage the file for the commit via "git add <filename>". The way I currently do this is by modifying the previous command line git diff bash call which entails deleting both the diff and any options which feels inefficient and a redundant approach.

How can I perform the diff and optionally the add together via the command line?

解决方案

To selectively apply diff hunks, use the -p aka --patch option. git add -p applies from worktree to index, git checkout -p applies from index or a named commit to index and worktree, git reset -p applies from a commit to the index.

Your options for each hunk are

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

which you get with ? or just hitting enter.

For just generally not repeating yourself at the command line, $_ expands to the previous command's last argument, so e.g. git diff --word-diff @ path/to/file and then just git add $_. There's also history expansion which pulls words from history lines not commands and varies slightly by shell, !$ is "the last word on the previous line", !#$ is "the last word on the line I'm typing now" so in-place sort is e.g. sort -nrk2,2 -o mylist !#$.

这篇关于简化"git add"操作在"git diff"之后,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆