Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
HOWTO: Using FileMerge (opendiff) with Git on OSX

HOWTO: Using FileMerge (opendiff) with Git on OSX

FileMerge (opendiff) can really come in handy when you need to visually compare merging conflicts. Other times it's just a nice visual way to review your days work.

The following method works by creating a simple bash script ( that sets us up with the proper command line arguments for Git to pass off files to FileMerge.

  1. Open the bash script for editing:

     vi ~/bin/
  2. Paste the following code:

     /usr/bin/opendiff "$2" "$5" -merge "$1"
  3. Make the bash script executable:

     chmod +x ~/bin/
  4. Tell Git (globally) to run our bash script when 'git diff' is issued:

     git config --global diff.external ~/bin/

Now head over to your Git-aware project directory and issue a git diff /path/to/modified/ and FileMerge will pop up showing you the differences against it and HEAD.

You can also do things like git diff --cached to review all the changes against HEAD.

yllan commented Oct 6, 2010

#!/bin/sh` should be


bkeating commented Oct 6, 2010

Thanks, yllan. Fixed.

yllan commented Oct 7, 2010

I think the leading "" of "#!/bin/sh" also need to be deleted. The correct version should be


/usr/bin/opendiff "$2" "$5" -merge "$1"


bkeating commented Oct 8, 2010

Thanks. Fixed it. Not sure why that was.

sebm commented Feb 1, 2011

This is awesome, thanks very much!

j-kan commented Mar 17, 2011

You can also use git difftool.


bkeating commented Mar 24, 2011

j-kan, blindly trying git difftool on another machine of mine did not do anything. shrug I'll keep it in mind though. Thanks.

j-kan commented Mar 25, 2011

Huh... here's what it does for me:

beton:pgm1 kan$ git difftool diagnosis.clj 
merge tool candidates: opendiff kdiff3 tkdiff xxdiff meld kompare gvimdiff diffuse ecmerge p4merge araxis emerge vimdiff

Viewing: 'pgm1/diagnosis.clj'
Hit return to launch 'opendiff': 

...and hitting return does exactly what it says. I'm running, if that matters....

gordonad commented Aug 8, 2012

Very confusing terminology. "Choose Left" moves the arrow from the right to the left. This seems that the code on the right goes into the chosen code on the left. - Please provide an improvement to help the user.

wamatt commented Aug 16, 2012

Anyone know what diff git natively uses? I like it's terminal output and want to make it my system default.

Hi, how do I paste the code in in step 2?
I try to paste it in, but I get an error that reads:

"E486: Pattern not found: usr"

I guess I don't understand how to use VI, it is not intuitive for me, but I want to make this work. Before I had git setup to use opendiff as the mergetool and it worked great. Somehow it stopped working and I don't know how to get it back. Thanks.


Frizlab commented May 17, 2013

There are two mode when you're in vi. The "command" mode in which you type commands, and the edition mode. To enter the edition mode, you should type "i", or "a". Then you can paste the code. To leave the edition mode, hit the escape key. To quit and save the changes, type ":x" then the return key while in command mode.

This works great but is there any way to make the git diff command line not wait until FileMerge quits?
I made the following modification so that a copy is made of the temp file that git generates so that & can be called on opendiff.

TMPFILE=$(mktemp /tmp/output.XXXXXXXXXX)
# Copy the temp file that git generates
# So that the command line can exit
cp $5 $TMPFILE
/usr/bin/opendiff "$2" "$TMPFILE" &

This works great when I test it directly from the shell.

./ ignored test.txt ignored ignored test2.txt 

but when asking git to do a diff I don't regain control of the terminal until I quit FileMerge

git diff 263f..4aaa complete.cpp 

After looking at this more closely it appears the git diff behavior is different depending on whether FileMerge is already running. So if FileMerge is running before a call to git diff then it works.

Here is a fish shell version for working with a temp file.


set -l TMPFILE (mktemp /tmp/output.XXXXXXXXXX)

# Copy the temp file that git generates
# So that the command line can exit
cp $argv[5] $TMPFILE

function event_git_diff --on-event git_diff_event
  /usr/bin/opendiff $argv[1] $argv[2] &

emit git_diff_event $argv[2] $TMPFILE

kwerle commented Jan 31, 2014

difftool did the right thing for me, too.

% git --version
git version (Apple Git-47)

Problem for me with difftool is that it will ask for merge file when saving.
Problem for me with this script is that opendiff will open new diffs before I had time to save/quit.

If someone knows how to fix sequential diffs where git will wait for the first file to be saved/quit before diffing another file + have merge path set I'd be really greatful.

I had this behaviour with svn using filemerge and:

But can't seem to replicate it with git.

@mcormier I think you can just put & after the command to put in the background.

For anyone interested, this is how I solved it (using git difftool):

    tool = opendiff
    prompt = false
[difftool "opendiff"]
    cmd = /usr/bin/opendiff \"$LOCAL\" \"$REMOTE\" -merge \"$MERGED\" | cat

jshado1 commented Sep 2, 2014

worked like a charm, thanks!

Thanks guys!
For me i preferred using difftool over external, since i have some other CLI tools that rely on std output from "git diff" command.

bblfish commented Apr 2, 2015

if you don't want opendiff to be used all the time, use git difftool ...

miner commented Apr 25, 2016

You can get a directory diff from git using the git difftool --dir-diff (or -d). I use that with a little bash script that invokes FileMerge. My script works around the issues with opendiff terminating too soon to save changes and not having the merge directory set.

git difftool -d -x gdiff

mfripp commented Oct 20, 2016

Similar to the advice from @nicholasruunu, you can use FileMerge with git by running these commands:

One-time setup:

git config --global diff.tool opendiff

Perform a diff with FileMerge:

git difftool

If you don't want to be prompted before opening each file, you can run this:

git config --global --add difftool.prompt false

Note: When using the git difftool option, you will have to quit FileMerge after each file is shown, and then git will show you the next one. In contrast, the original script above opens all the files at once.

By the way, if you want to undo the original setup, you can run this command:

git config --global --unset-all diff.external

If you want to undo the difftool setup, you can run these commands:

git config --global --unset-all diff.tool
git config --global --unset-all difftool.prompt

I think you can also use FileMerge for merges via this command (see, but I haven't tried it:

git config --global merge.tool opendiff

After doing the above, you can open all the diffs at once (instead of one file at a time) by doing:

git difftool --dir-diff <other_branch>

And if you do this a lot you can setup an alias in ~/.gitconfig:

        dt = difftool --dir-diff

Then you can just do:

git dt <other_branch>

For me, with git 2.16.1 it's enough to just configure an alias:

    opendiff = difftool --no-prompt --tool opendiff --dir-diff

Then run

git opendiff <other_branch>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment