Resolving Merge Conflicts in Git

Meng Lin, in 11 August 2014
Merging conflicts, as one of the most traumatising experience any dev can have, is the process we all seek to avoid. But with the help of Git, everything can be done so elegantly yet painless.

Relax, you can’t make it worse

Yes, despite the fear and discomfort we all suffer on this topic, first thing to remember is that you literally can’t make matters worse in Git. As notorious as it can be like a tree conflict in SVN, Git has already taken care of a lot of funky scenarios, leaving you a peace of mind while merging. Also, as a last resort, you always have the choice to undo a merge.

So don’t worry.

How do conflicts happen?

A typical example could be the changes are made to the same part of code in the same file on two branches.

origin a <--- b <--- c
  |                  ^HEAD
  |
 foo   a <--- b <----- d

When we try to merge ‘foo’ branch into ‘origin’ branch,

$ git checkout master
$ git merge foo

We would receive a lovely message saying ‘CONFLICT’, and the merge should fail miserably.

How to deal with the conflicts?

Supposingly, you can go blame the other people an incompetent dev, or a complete nutcase.

Blame game
Blame game

Since there is only one in a million chance for merge conflicts to happen, I rather take this as an opportunity to unleash the power of dragon.

Open up the file with conflicts in any editor of your choice, you will see things like:

<<<<<<< HEAD
a line in master branch, and should remain here.
=======
This is now a line in foo branch
>>>>>>> foo

Now, cleaning up the bits you don’t want is really the matter of patience. What you’ve got left to do is to finish the merge by doing more or less like a normal commit.

$ git add <file>
$ git commit -m '<message>'

And you will actually have both commits from the two branches, and the HEAD is now sitting at the merge commit.

origin a <--- b <--- c <--- d < ---- e
  |                       /          ^HEAD
  |                      /
 foo   a <--- b <----- d

What if I make poo poo in the merge?

As promised, you can undo the merge from the point of merge up to committing the changes, by:

$ git merge --abort

The worst comes to the worst that you want to retract whatever merge is committed, you can still reset the repository to where it was before by:

$ git reset --hard <commit>

There’s more to come… Rebase