Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does git revert fail on this example?

Tags:

git

A coworker was playing with git revert today he ended up on a weird situation:

git init
echo 1 > file.txt
git add file.txt
git commit -m "Commit 1"
# Say this generates hash aaa

cat file
1

echo 2 >> file.txt
git add file.txt
git commit -m "Commit 2"
# Say this generates hash bbb

cat file
1
2

echo 3 >> file.txt
git add file.txt
git commit -m "Commit 3"
# Say this generates hash ccc

cat file
1
2
3

git revert bbb
#line above does not work

The revert does not work and ends up on a cherry-pick situation. I was expecting the following result:

cat file
1
3

diff -R works fine

diff --git b/file.txt a/file.txt
index aaa..bbb 100644
--- b/file.txt
+++ a/file.txt
@@ -1,2 +1 @@
 1
-2

It is probably something really stupid, but what?

like image 381
Mac Avatar asked Oct 18 '22 21:10

Mac


1 Answers

It's just the reverse of a merge conflict. An un-merge conflict? You can think of a revert as the opposite of a merge. You would get a merge conflict if you tried to do the same thing in the opposite direction.

$ git init
Initialized empty Git repository in /home/depp/test/.git/
$ echo 1 > file.txt
$ git add .
$ git commit -m A
[master (root-commit) 406d008] A
 1 file changed, 1 insertion(+)
 create mode 100644 file.txt
$ echo 2 >> file.txt
$ git add .
$ git commit -m B
[master 730fed9] B
 1 file changed, 1 insertion(+)
$ git branch branch1 'HEAD^'
$ git checkout branch1
Switched to branch 'branch1'
$ echo 3 >> file.txt
$ git add .
$ git commit -m C
[branch1 c359c04] C
 1 file changed, 1 insertion(+)
$ git merge master
Auto-merging file.txt
CONFLICT (content): Merge conflict in file.txt
Automatic merge failed; fix conflicts and then commit the result.

You may think that the answer is "obvious" and you might even want to file a bug or enhancement request with the Git developers, but Git doesn't think the answer is obvious. Git thinks that you're supposed to put "3" after "2", but now there's no "2" and so where are you supposed to put the "3"?

It makes a certain amount of sense that Git would ask you for help if the context for a certain change does not exist, because these are the cases where you might want to do a manual fixup.

Union merge?

I tried specifying merge=union in the .gitattributes for file.txt, but this ended up making the revert not do anything at all. Weird.

like image 87
Dietrich Epp Avatar answered Oct 21 '22 22:10

Dietrich Epp