Sunday, September 20, 2015

How to solve Git merge conflicts using kdiff?

KDiff's windows (or any other three way merge tool's windows) aren't quite so intuitive, so here's some help.

Let's say you've worked on the master branch

and somebody else worked on a "tryingOutSomething" branch

...and you decide to merge that person's changes into your code.

But Git complains that there are merge conflicts!
Turns out the other person was working on the same file you were working on, and also on the same lines.

GitExtensions refers you to KDiff3 to solve the merge conflicts. Select the option to start merging and you see a screen with four sections.

Click image to view larger
The sections from right to left are:

  • Section C: The code of the branch you are going to merge into your branch.
  • Section B: The code of your current branch.
  • Section A: The code that is in the commit which is a common ancestor of B and C. Don't bother about section A. You'd hardly ever use it.
  • Bottom section: It's what your code will look like once you've done the merges and save the file.

You can manually make changes or copy code and paste it into the bottom section like this:

Or, note that the extreme left of the bottom section is segregated into areas where there are merge conflicts and it shows letters A, B, C or m.

Any letters that are there before you started merging, are merges that happened automatically. If that's not what you wanted to merge, then click on the letter in the bottom section and select amongst A, B or C in the toolbar on top.

You can even edit the automatically merged conflicts and choose A, B or C or edit it manually.

That's it. Now just save and exit, and Git will allow you to commit.


You'll see a residual file of the merge, which has a ".orig" extension. Don't commit it. Just delete it.
In case your merging went wrong and Git says "You have not concluded your merge (MERGE_HEAD exists)", then just go into the hidden ".git" folder and delete the files named:
After deleting those files, you'll be able to do the merging again. Do it carefully this time and save and exit.

All the best! :-)

No comments: