该git add -p命令允许对文件或部分文件进行交互式分段(https://git-scm.com/book/en/v2/Git-Tools-Interactive-Staging).
有没有办法以非交互方式登台?说,我有这些帅哥:
$ git diff
diff --git a/test.txt b/test.txt
index 77e67ac..34eabb1 100644
--- a/test.txt
+++ b/test.txt
@@ -1,4 +1,4 @@
-this is the first change
+this is the first change, don't stage it!
@@ -6,5 +6,5 @@ this is the first change
-this is the first change
+this is the second change, stage it!
Run Code Online (Sandbox Code Playgroud)
如果git add -p 2不通过交互式菜单运行第二个hunk ,那么运行一个命令会很不错.
注意:这里提到了一个解决方案(如何在git中以非交互方式调度块?),但它涉及多个命令以及编辑补丁文件的附加步骤.
这什么时候有用?假设我正在编辑一个冗长且重复的JSON文件,并且在运行之后git diff,我知道我想要将所有其他大块文件放在一起.我不想单独检查每个大块头; 我只想告诉git"阶段第2,第4,第6,第8等等."
当尝试使用自定义脚本与Sourcetree(可以使用帅哥)进行交互时,这也很有用.
如果您能够手动或自动(例如,感谢sed)编辑您的补丁(感谢 生成的补丁git diff),您可以通过以下命令以非交互方式暂存 hunks:
git apply --cached patchfile
Run Code Online (Sandbox Code Playgroud)
PS:我实际上可以在这里找到您问题的答案:/sf/answers/2204913791/