How To Use DiffMerge

mainTo people who don’t know what merge conflicts are, they are these magical things that make our life pleasant when we are coding in a group. Okay that was a joke! Too subtle? Anyway, when you are working in a team on a central code base, multiple people might end up modifying the same thing, which usually results in a conflict. Merge conflicts are bound to happen when you are working in parallel with a team of developers. If it hasn’t happened to you yet, then you haven’t been coding long enough. Most of the version management tools try to manage this automatically. If people are working on different things that are independent of each other, then there will be no problems. But unfortunately, we are not in utopia. Here on earth, conflicts happen all the time.  

When Git cannot automatically merge changes, a merge fails and it will show warning messages. The conflicted file gets filled with these annoying signs – “<<<<<<<“, “=======”, “>>>>>>>”. The only solution when this happens is to merge them manually. Luckily, to make the merge less painful, there are visual tools that assist us in doing this. DiffMerge is one such tool and Git allows using it as opposed to using the default tool that comes with Git.

Installing DiffMerge

DiffMerge is great to perform 3-way merges as it supports 3-way file comparison. The three versions being the old one, the current one and the new one that will go into the repo. 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. I will be describing how to set it up in Mac OSX, but the rules are more or less the same for any Unix-style system. Before we start, make sure you download the DiffMerge OSX installer from here.

Once you run the installer, just type the following on your command line to see if diffmerge works:

>> diffmerge

You should see the DiffMerge window pop up. If you want to compare two random files, type the following on the terminal:

>> diffmerge file1.py file2.py

You should see a window pop up with these two files being displayed side-by-side. You will also see the differences being highlighted.

Setting up DiffMerge for Git

Now that we have DiffMerge, let’s make it our default merge tool for git. Run the following commands in your terminal:

>> 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

DiffMerge is now the default tool. To start a three-way merge and perform a visual merge for each file in conflict, run the following command:

>> git mergetool

Diff Tool Configuration

Sometimes you may want to compare two revisions to see what’s changed. These tools are called diff tools and are commonly used to compare changes between revisions. You can setup Diffmerge to be your visual diff tool 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 file1.py

———————————————————————————————–

One thought on “How To Use DiffMerge

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s