How to setup Git to use Diffmerge

Diffmerge

Merge conflicts, they are bound to happen sooner or later when you are working in parallel with a team of developers. When Git cannot automatically merge changes, a merge fails and the conflicted file becomes littered with <<<<<<<, ===, and >>>>>>> markers. To ease the pain of having to performing merges, Git allows using one of many supported merge tools or you can configure Git to use the tool of your choice instead.

In this post, I am going to show you how to do the latter in Mac OS X using my preferred merge tool, Diffmerge. Diffmerge is a free graphical file comparison tool by SourceGear available in Mac OS X, Linux, and Windows. It is ideal to perform 3-way merges as it supports 3-way file comparison. During a conflict, you are presented with the version of the branch you are working on, the common file ancestor, and the branch you are merging in.

Visual Mergetool Configuration

Running the following commands in your terminal will set Diffmerge as your default visual mergetool:

git config --global merge.tool diffmerge

git config --global mergetool.diffmerge.cmd "diffmerge --merge
--result=\$MERGED \$LOCAL \$BASE \$REMOTE"

git config --global mergetool.diffmerge.trustExitCode true

As of the 3.3 release of Diffmerge, exiting a visual merge will return the correct status to Git. Prior to this release, Diffmerge would always return 0, indicating that the merge was resolved even on aborted merges. When you attempt to close Diffmerge, you will be prompted to choose if the merge is to be aborted or is resolved.

To start a 3-way merge with your visual mergetool, run:

git mergetool

This will perform a visual merge for each file in conflict. You may also provide an optional file parameter, to perform a merge on a specific file.

Don’t store backups

By default, Git will store a backup of the original file with conflict markers after a successful merge. These files will have a .orig extension added to the original filename. When I configure Git, I ensure that these files are not saved. To globally disable backups of the original merge file, run the following:

git config --global mergetool.keepBackup false

Visual Difftool Configuration

You may also setup Diffmerge to be your visual Difftool by running the following commands in your terminal:

git config --global diff.tool diffmerge
git config --global difftool.diffmerge.cmd "diffmerge \$LOCAL \$REMOTE"

This will allow you to compare two different versions of files visually. For example:

git difftool master~1 foo.rb
blog comments powered by Disqus