admin管理员组

文章数量:1394099

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)

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)
Share Improve this question edited Mar 14 at 7:03 Tom Huntington asked Mar 13 at 22:27 Tom HuntingtonTom Huntington 3,45514 silver badges33 bronze badges 4
  • This is probably a dup of multiple questions, one of which could be: Reverting a specific old commit. – TTT Commented Mar 14 at 6:20
  • 2 You cannot avoid a conflict no matter what you tried. When you committed B, the line B was added between A and and EOF. When you committed C, C was added between B and EOF. If you try to revert B, B would try to be taken back between A and EOF which is not there anymore. You have a C now after B so git has to show you conflict markers. – eftshift0 Commented Mar 14 at 7:17
  • 2 If you want to try this reversal experiment with something more realistic that does not automatically end up in a conflict, try with a file that has numbers 10 to 20, a line per number in the root commit, then add 4 letters between 4 different numbers in 4 commits.... then try reverting one of them, it won't fail (because git is able to see the before/after markers for the content you want to remove). – eftshift0 Commented Mar 14 at 7:23
  • 1 @eftshift0 thanks! I did try with double new lines before editing and it didn't auto-merge

    本文标签: gitUndo commit B keeping commits C and DStack Overflow