gitgit-rebase

Undo commit B keeping commits C and D


Say my commit history looks like this:

A -- B -- C -- D (HEAD)

With a file test.txt, where each letter was added in the respective commit

A
B
C
D

Without changing the git history (just changing the working directory), I would like to undo the changes of commit B, while keeping the changes of commits C and D. If there is a conflict, add in the "conflict dividers"

diff --git a/t.txt b/t.txt
index 8422d40..d9a85ba 100644
--- a/t.txt
+++ b/t.txt
@@ -1,4 +1,3 @@
 A
-B
 C
 D

edit

git revert -n B was what I tried, but it just put all the changes in C and D into a conflict...

$ git revert -n 440e9c
Auto-merging t.txt
CONFLICT (content): Merge conflict in t.txt
error: could not revert 440e9c1... B
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'

$ git diff
diff --cc t.txt
index 8422d40,760a73a..0000000
--- a/t.txt
+++ b/t.txt
@@@ -1,4 -1,2 +1,8 @@@
  A
++<<<<<<< HEAD
 +B
 +C
 +D
++=======
+
++>>>>>>> parent of 440e9c1 (B)

Solution

  • git revert commit_B_hash
    

    Maybe you need to resolve the conflicts manually.The commit history remains unchanged, still being A -- B -- C -- D, but the changes made in commit B are reverted, retaining only the changes from commits C and D.