Search code examples
gitmergegit-mergegit-revert

Git merge finds conflict after reverting to common version between branches


I have two branches called test and prod-candidate. These two branches have a common commit in their history, a0e378cfe28, which was originally in the test branch and was later merged into prod-candidate. After this commit, the two branches went different ways and had other commits associated to them.

It is now time to merge test into prod-candidate again, so I was hoping that by reverting all the commits in the prod-candidate branch back to commit a0e378cfe28 I would be able to do the merge without problems. I did the revert and by running git diff I was able to verify that the revert was successful because the prod-candidate branch has the exact same contents as commit a0e378cfe28.

Unfortunately, after checking out prod-candidate and running git merge test, I am getting a conflict in the POM file and I can't understand why. The test branch has a POM with version 18.4.0-101, and prod-candidate, which before the revert had version 18.3.0-204, currently has version 18.3.0-101 (which is the same as commit a0e378cfe28).

Why can't I merge the branches after reverting to a common base commit?


Solution

  • It sounds like you are doing this the hard way. If you want the prod-candidate branch to contain the same changes as the test branch, the easiest way is to do this:

    git checkout prod-candidate
    git reset --hard test
    

    This will effectively move prod-candidate to point at the same commit as test. This means that it will have the exact same revision history. You won't have any merge conflicts to worry about, either.

    Note

    git revert will create a new commit that reverses the changes of the specified commit. If you revert multiple commits, one at a time, the end result is a chain of commits that undoes the changes in the previous commits. Even though you end up with the same content on both branches, they have two different histories. You are getting merge conflicts because a line was changed in different ways on both branches. Git does not detect that the line of code in prod-candidate has been changed back to the same content as the shared parent commit.