I'm manually reviewing a huge number of changes (in the 1000s) made by a search and replace script over some message catalogs. At the moment I'm doing git add -p
, but I keep taking breaks to check other files or adjust the script, so I'm alternating that with git checkout -p
to discard the changes I don't want. Is there a way to combine the two? I.e. for each hunk I want the option to stage it, or discard it.
5 回答
如果您使用的是 Windows 或 Mac OS,您可以使用免费的 SourceTree GUI 应用程序,该应用程序允许您在差异视图中暂存或丢弃每个块(甚至从每个块中选择行)。
how about typing j - leave this hunk undecided, see next undecided hunk
for hunks which you don't want to stage,
and after all of these, run git checkout --
, then those hunks selected will be staged, and other discarded.
when you are using git add -p
, typing ?
will show the help
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 gui吗?这是一个与 git 捆绑在一起的实用程序,它试图让复杂的操作变得容易,这些操作混合了暂存和结帐。你可以在这里阅读
您可以选择执行一个大块并放弃其余的更改。通过这样做ctrl + j
,cmd + j
您可以签出所选文件。
我知道从 shell 中使用 git 会快得多,但在像这样的复杂情况下,在不同命令之间切换会成为一种开销。
这与 Git 2.28(2020 年第三季度)更接近!
以前,“ git checkout -p
”根本不处理新添加的路径。
请参阅Johannes Schindelin ( ) 的提交 2c8bd84(2020 年 5 月 27 日)。(由Junio C Hamano 合并 -- --在提交 2bdf00e中,2020 年 6 月 9 日)dscho
gitster
checkout -p
: 正确处理新文件报告人:Merlin Büge
帮助人:Jeff King
签字人:Johannes Schindelin最初的补丁选择代码是为 编写的
git add -p
,它工作的基本单元是一个大块。我们绕过它来处理24ab81ae4d中的删除(“
add-interactive
:处理空文件的删除”,2009-10-27,Git v1.6.6-rc0 -- merge)。
但git add -p
永远不会看到新文件,因为我们只考虑索引中跟踪的文件集。但是,由于
git checkout -p
&friends 使用了相同的机器,我们可以看到新文件。专门处理这个案例,为其添加一个仿照
deleted file
案例的新提示。这也解决了添加的
_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,?]?
如果你丢弃你不想要的东西,并提交你需要的东西,那么 agit add $(git ls-files -o --exclude-standard)
将添加新的剩余文件。
暂存您需要的所有内容,提交然后重置或存储以清理工作目录以摆脱您不需要的内容。