如何删除应用的 git 补丁?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34400885/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 03:46:46  来源:igfitidea点击:

How can I remove an applied git patch?

gitgithub

提问by coldy

I have a git repo, where we apply many patches in test environment.

我有一个 git repo,我们在测试环境中应用了许多补丁。

git apply --stat --check --ignore-whitespace /home/kent/Desktop/patches/test.patch --exclude .gitignore

git am -s --ignore-whitespace /home/kent/Desktop/patches/test.patch --exclude .gitignore --exclude .gitignore

If I have to remove the patch and apply a new one, at present I clone the live content and reapply all the test patches and push again. This process is somehow cumbersome and also leads to errors at times I also miss one or two patches.

如果我必须删除补丁并应用新补丁,目前我会克隆实时内容并重新应用所有测试补丁并再次推送。这个过程有点麻烦,有时也会导致错误,我也会错过一两个补丁。

I wanted to know if there is a way to remove a patch and apply the new one

我想知道是否有办法删除补丁并应用新补丁

Also, to add one way is there if we commit each time to the patch and then i can use:

另外,如果我们每次提交补丁,添加一种方法,然后我可以使用:

git revert <<commit id>>

The above does not work for me at all times.

以上在任何时候都不适合我。

回答by ash

TL;DR

TL; 博士

You can revert a patch with:

您可以使用以下命令还原补丁:

$ git apply -R <patch>


You can generate a patch either by one of the following:

您可以通过以下任一方式生成补丁:

This will generate a patch from a diff

这将从差异生成补丁

$ git diff --patch > 0001-some-modifications.patch

If you want to generate a patch for just the HEAD commit:

如果您只想为 HEAD 提交生成补丁:

$ git show --patch HEAD^ > 0001-some-modifications.patch

You can generate a patch for the previous 3commits from HEAD:

您可以为HEAD的前3 次提交生成补丁:

$ git show --patch HEAD~3 > 0001-some-modifications.patch

You can apply the patch by:

您可以通过以下方式应用补丁:

$ git apply -- 0001-some-modifications.patch

You can revert a patch with:

您可以使用以下命令还原补丁:

$ git apply -R <patch>

When you generate a patch it is just a diff with metadata; files, line numbers adds/removes; something along the following:

当您生成补丁时,它只是与元数据的差异;文件、行号添加/删除;以下内容:

commit 9dad147cbf16befecdef2e812c1249499bdef5ac
Author: My Name <[email protected]>
Date:   Mon Dec 21 20:46:01 2015 +0000

    Example commit message.

diff --git a/src/example.md b/src/example.md
new file mode 100644
index 0000000..ab73512
--- /dev/null
+++ b/src/example.md
@@ -0,0 +1,3 @@
+# Example document
+
+ Hello World

So when you use git applyyou're essentially applying the edits as per to the tree.

因此,当您使用时,您git apply实际上是在将编辑内容应用到树上。

When you then run git apply -Rgit will simply do the opposite to the patch.

当你运行git apply -Rgit 时,它只会做与补丁相反的事情。