git add -p
,但是我不断休息来检查其他文件或调整脚本,所以我在交替使用git checkout -p
来放弃我不需要的更改。有没有一种方法可以将这两者结合起来?也就是说,对于每个 hunk,我希望有选项将其暂存或放弃。git add -p
,但是我不断休息来检查其他文件或调整脚本,所以我在交替使用git checkout -p
来放弃我不需要的更改。有没有一种方法可以将这两者结合起来?也就是说,对于每个 hunk,我希望有选项将其暂存或放弃。你有没有看过git gui?它是现在与git捆绑在一起的实用程序,旨在使混合暂存和检出的复杂操作变得容易。你可以在这里阅读相关信息。
你可以选择暂存一个块并丢弃其余更改。通过执行ctrl + j
或cmd + j
,你可以检出所选文件。
我知道使用shell中的git速度更快,但在这种复杂情况下,切换不同命令会变得繁琐。
如果你不想提交某些补丁,可以尝试在敲击 j
后离开当前未决定的补丁并查看下一个未决定的补丁。然后,在执行 git checkout --
命令后,被选中的补丁将会被暂存,而其他的则会被忽略。
当你使用 git add -p
命令时,输入 ?
将显示帮助信息。
Stage this hunk [y,n,q,a,d,/,e,?]?
y - stage this hunk
n - do not stage this hunk
q - quit, do not stage this hunk nor any of the remaining ones
a - stage this and all the remaining hunks in the file
d - do not stage this hunk nor any of the remaining 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
这个功能将在 Git 2.28(2020 年第三季度)中更加接近!
之前,“git checkout -p
”根本不处理新添加的路径。
请参见提交记录 2c8bd84(2020 年 5 月 27 日),作者为Johannes Schindelin(dscho
)。
(由Junio C Hamano -- gitster
--于提交记录 2bdf00e中合并,2020 年 6 月 9 日)
checkout -p
:正确处理新文件报告者:Merlin Büge
协助者:Jeff King
签署者:Johannes Schindelin
原始的修补程序选择代码是为git add -p
编写的,它所作用的基本单位是一组 hunk。
我们曾经在24ab81ae4d(“add-interactive
: handle deletion of empty files”,2009-10-27,Git v1.6.6-rc0 -- merge)中修改它以处理删除。但是,由于我们只考虑索引中跟踪的文件集,因此git add -p
永远不会看到新文件。
但是,由于相同的机制被用于git checkout -p
等操作,因此我们可以看到新文件。
专门处理此情况,添加一个新提示,该提示是根据“删除的文件”情况建模的。
这还解决了添加的_empty
_文件无法通过git checkout -p
进行暂存的问题。
现在对于git checkout -p
的提示包括:
Discard mode change from worktree [y,n,q,a,d%s,?]?
Discard deletion from worktree [y,n,q,a,d%s,?]?
Discard addition from worktree [y,n,q,a,d%s,?]? <===
Discard this hunk from worktree [y,n,q,a,d%s,?]?
git add $(git ls-files -o --exclude-standard)
将会添加新的剩余文件。暂存所有需要的内容,提交更改后重置或者储藏以清理工作目录,摆脱不必要的内容。
git add -p
选择我想要在第一次提交中包含的一些块。切换到git checkout -p
以丢弃一些块。添加更多内容。提交,然后为第二次提交添加所有剩余的内容。如果能够将 add 和 checkout 结合起来就太好了! - Tieme