Identifying GIT Merge Conflicts with a Merge Tool

  • Sharebar

One of the pain points in GIT was resolving merge conflicts. I have highlighted ‘was’ because at the end of this blog you will find your life easy the next time you encounter merge conflicts. Working with GIT in agile development increases the probability of merge conflicts, and the reason behind is straight forward – Parallel Development.  As I mentioned in my earlier blog, there are lot of advantages of using GIT branching model. But one of the pain points I mentioned was resolving merge conflicts. One has to manually open the files and identify conflicts. In this blog, I will share how you can configure and open a 3-way merging tool to identify your merge conflicts.

Why a 3-way merging tool? You always have two branches that are involved in merging. But you might want to look at the common ancestor as well. A 3-way merging tool helps you in viewing and comparing the conflicts with your remote ancestor branch (master, for instance). Please download any 3-way merge tool before moving ahead.

Note: WinMerge is not a 3-way merge tool. I will be using DiffMerge here.

So the next time you see any merge conflict, please follow the steps below:

Type git mergetool. You will see the default merge tool candidates.

You might feel relaxed to see that GIT is offering couple of tools for identifying conflicts. Unfortunately, they all are command-line based tools so we will be adding a visual tool to it, and next time when you write a mergetool command, you will see your own visual tool opened up with the file.

You might get a hint that we need to override the ‘mergetool’ so go ahead and open your .gitconfig file present in C:\Users\{user-name}\ (That’s for Windows 7 by the way). You will find tags like [gui], [user], etc. Add following lines to it:


[merge]

tool = diffmerge

[mergetool "diffmerge"]

cmd = git-merge-diffmerge-wrapper.sh "$PWD/$LOCAL" "$PWD/$REMOTE" "$PWD/$BASE"

You can specify the name of the mergetool you are using inside the [mergetool] tag, but make sure it matches the ‘tool’ property of [merge] tag. You can see [mergetool] requires git-merge-diffmerge-wrapper.sh. Download it and paste it inside {GIT_INSTALLATION_FOLDER}\cmd. Please change the extension of the file to .sh. You should have something like below:

Also, please add {GIT_INSTALLATION_FOLDER}\cmd in your PATH.

You can enter the path of the executable of the diff tool you wish to use in git-merge-diffmerge-wrapper.sh.

And you are done! Go back to your GIT Bash and re-enter git mergetool. You will see the name of your diff tool.

Once you hit ENTER, you can see your diff tool opening the file with three views. I dint get a way to find the branch names involved in the merging so I am referring the title of the views as CURRENT, BRANCH BEING MERGED and ANCESTOR. CURRENT – the branch accepting the merge, BRANCH BEING MERGED – as the name says and ANCESTOR – the remote copy of the file on origin.

If you have more than 1 file with conflicts, then your diff tool will be opened for each file. Once you fix the conflict, you can save the file and close the tool. You can then ADD and COMMIT the files.

Hope you can now indentify merge conflicts easily.

Share the bee buzz:
  • Digg
  • del.icio.us
  • Facebook
  • DZone
  • LinkedIn
  • StumbleUpon
  • Technorati
  • Twitter

Leave a Reply