Are you using Git in Ubuntu and want to use an external visual diff viewer? It's easy! I will be using Meld for this example but most visual diff tools should be similar. If you don't already have Meld, then install it:

sudo apt-get install meld

Ok. Now let's begin by breaking it. Enter this into a terminal:

git config --global diff.external meld

Then navigate to a Git tracked directory and enter this (with an actual filename):

git diff filename

Meld will open but it will complain about bad parameters. The problem is that Git sends its external diff viewer seven parameters when Meld only needs two of them; two filenames of files to compare. One way to fix it is to write a script to format the parameters before sending them to Meld. Let's do that.

Create a new python script in your home directory (or wherever, it doesn't matter) and call it diff.py.

#!/usr/bin/python

import sys
import os

os.system('meld "%s" "%s"' % (sys.argv[2], sys.argv[5]))



Now we can set Git to perform it's diff on our new script (replacing the path with yours):

git config --global diff.external /home/nathan/diff.py
git diff filename


This time when we do a diff it will launch Meld with the right parameters and we will see our visual diff.

4 Comments

Travis Parker
2:24am, 18 June 2008

python is a bit overkill for t his problem. this should do the trick just as well:

#!/usr/bin/env sh
meld $2 $5

Travis Parker
2:38am, 18 June 2008

does meld do multiple-file diffs at once? it is a little wierd having meld go up and down over and over again for each file. is this a git-diff issue?

Thanks for the tip

Corey
8:41am, 15 July 2008

Thanks Nathan! I used your tip to get Git to use KDiff3 on OS X. Works great except that you need " characters around the string arguments so KDiff3 doesn't choke on spaces in file paths.

os.system('meld "%s" "%s"' % (sys.argv[2], sys.argv[5]))


Nathan
12:51pm, 20 July 2008

Thanks Corey! I fixed up the script to wrap the args in "".

Post a comment

Name
Link
Email (not shown, used for avatar)
Type this into the box: