How can I remove an applied git patch?
I have a git repo, where we apply many patches in test environment.
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.
TL;DR
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:
$ git show --patch HEAD^ > 0001-some-modifications.patch
You can generate a patch for the previous 3 commits from HEAD:
$ 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 apply
you're essentially applying the edits as per to the tree.
When you then run git apply -R
git will simply do the opposite to the patch.
On Windows
I'm using git on Windows and it works a bit differently than on Linux. Specifically, I found that when I ran:
git apply -R C:\downloads\mypatch.patch
This perhaps was running in the wrong directory. I had to copy it to the local directory where I was applying the patch. I also got errors like:
error: product/build.gradle: No such file or directory
error: main/generator/generator.js: No such file or directory
Which are super weird because those files are in directories specified inside of my patch. The only fix I could find for these was to modify the patch by hand. I had to change the filenames to prepend ./
to the directories and filenames. So I turned this:
Index: build_scripts/product/build.gradle
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- build_scripts/product/build.gradle (revision 537fbcc1ebdf65652896eaaf2a315cc44a24ba6c)
+++ build_scripts/product/build.gradle (date 1594123740523)
@@ -304,7 +304,7 @@
from("$repoRootDir/src/main/nodejs/utils/") {
include "common_*.js"
}
Into this:
Index: ./build_scripts/product/build.gradle
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- ./build_scripts/product/build.gradle (revision 537fbcc1ebdf65652896eaaf2a315cc44a24ba6c)
+++ ./build_scripts/product/build.gradle (date 1594123740523)
@@ -304,7 +304,7 @@
from("$repoRootDir/src/main/nodejs/utils/") {
include "common_*.js"
}
The only change is the ./
before the 3 different build_scripts/product/build.gradle
strings.
Also, another nice hint is that git apply
takes a -v
parameter that gives a little more verbosity so you can see that it's first checking to see if the patch is safe to apply before applying it.