James Lin
James Lin

Reputation: 26588

Why goes git revert result in conflicts?

Lets day I have the following commits in git log:

previous commits --> bug fix --> pep8 the whole file (head)

-----------------------------------------------------
commit abcdefg2
pep8 the whole file

commit abcdefg1
bug fix

Now that I want to revert the pep8 changes by running:

git revert abcdefg1

Then I get a lot of conflicts... I wonder what I have done? I am not sure which commit to use in the revert, abcdefg1 or abcdefg2?

Upvotes: 1

Views: 314

Answers (2)

jthill
jthill

Reputation: 60565

git revert and git cherry-pick are merge operations.

For cherry-pick, the cherry-picked commit's parent is the base, the cherry-picked commit is the "their" tip, your checkout is the "our" tip.

...B---C     git cherry-pick C

...H         with H the HEAD 

is a merge with B as the base, it merges the changes from B to C with the changes from B to H. This might take some quiet time to appreciate, it's worth spending it.

Revert is simply the reverse:

...T---R    git revert R

...H        with H the HEAD

merges the changes from R to T with the changes from R to H.

Upvotes: 1

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522741

Here is a diagram showing the state of your branch after applying the revert commit:

branch: ... I -- A -- B -- C

Here commit A is pep8 the whole file, B is the bug fix, and C is the revert of commit A. However, functionally speaking, reverting commit A leaves your branch in the same state as the following:

branch: ... I -- B

In other words, after applying the revert commit it is as if you had just laid down the bug fix on top of some earlier commit. Most likely, this resulted in some conflicts which must be resolved. You could test this theory yourself, or just accept that it is perfectly normal for a conflict to happen when doing a revert.

Upvotes: 1

Related Questions