在git中非交互地执行大块 [英] Stage hunk non-interactively in git

查看:39
本文介绍了在git中非交互地执行大块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

git add -p 命令允许交互式暂存块或文件的一部分(

The git add -p command allows interactive staging of hunks or parts of a file ( https://git-scm.com/book/en/v2/Git-Tools-Interactive-Staging).

是否有一种非互动方式上演大块头的方法?说,我有这些帅哥:

Is there a way to stage a hunk non-interactively? Say, I have these hunks:

$ 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!

最好运行单个命令,例如 git add -p 2 来执行第二个大块,而无需通过交互式菜单.

It would be nice to run a single command like git add -p 2 to stage the second hunk without going through the interactive menu.

注意:此处提到了一种解决方案(

NOTE: One solution is mentioned here (How to stage chunks non-interactively in git?), but it involves multiple commands and an additional step of editing a patch file.

这什么时候有用?说我正在编辑一个长且重复的JSON文件,并且在运行 git diff 之后,我知道我想对其他所有块进行暂存.我不想单独经历每个大块;我只想告诉git演出第二,第四,第六,第八等大块头".

When would this be useful? Say I was editing a long and repetitive JSON file, and after running git diff, I know I want to stage every other hunk. I don't want to go through each hunk individually; I just want to tell git "stage the 2nd, 4th, 6th, 8th, etc. hunks".

当尝试使用自定义脚本与Sourcetree交互时(它可以处理块),这也很有用.

This can also be useful when trying to interact with Sourcetree (which enables working with hunks), using self-defined scripts.

推荐答案

如果您能够手动或自动(例如,感谢 sed )编辑补丁(感谢您使用 git diff ),由于以下命令,您可以非交互式地播放帅哥:

In case you are able to manually or automatically (say, thanks to sed) edit your patch (the one produced thanks to git diff), you can stage hunks non-interactively thanks to the following command:

git apply --cached patchfile

PS:我实际上可以在这里找到您问题的答案: https://stackoverflow.com/a/31498768/7009806

PS: I could actually find the answer to your question here: https://stackoverflow.com/a/31498768/7009806

这篇关于在git中非交互地执行大块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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