Skip to content

Instantly share code, notes, and snippets.

@bkeating
Created March 11, 2010 21:36
Show Gist options
  • Save bkeating/329690 to your computer and use it in GitHub Desktop.
Save bkeating/329690 to your computer and use it in GitHub Desktop.
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 (git-diff-cmd.sh) 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/git-diff-cmd.sh
    
  2. Paste the following code:

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

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

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

Now head over to your Git-aware project directory and issue a git diff /path/to/modified/file.py 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.

@mcormier
Copy link

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.

#!/usr/local/bin/fish

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] &
end

emit git_diff_event $argv[2] $TMPFILE

@kwerle
Copy link

kwerle commented Jan 31, 2014

difftool did the right thing for me, too.

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

@nicholasruunu
Copy link

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.

Update:
I had this behaviour with svn using filemerge and:
http://soft.vub.ac.be/svn-gen/bdefrain/fmscripts/fmdiff

But can't seem to replicate it with git.

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

@nicholasruunu
Copy link

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

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

@JakobJingleheimer
Copy link

worked like a charm, thanks!

@christopherscott
Copy link

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

bblfish commented Apr 2, 2015

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

@miner
Copy link

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.

https://gist.github.com/miner/e73fc98a83a8fe05d9ef000d46d68a9f

git difftool -d -x gdiff

@mfripp
Copy link

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 http://stackoverflow.com/a/20123093/3830997), but I haven't tried it:

git config --global merge.tool opendiff

@robrohan
Copy link

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:

[alias]
        dt = difftool --dir-diff

Then you can just do:

git dt <other_branch>

@davidparsson
Copy link

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

[alias]
    opendiff = difftool --no-prompt --tool opendiff --dir-diff

Then run

git opendiff <other_branch>

@gingerbeardman
Copy link

Thanks @mfripp

@brodybits
Copy link

Even quicker config, works for me on git 2.15.2:

git config --global alias.opendiff difftool --no-prompt --tool opendiff --dir-diff

@jaredatron
Copy link

git config --global alias.opendiff difftool --no-prompt --tool opendiff --dir-diff

this did not work for me. just outputs the usage text.
I'm using:

$ git --version
git version 2.19.0
hub version 2.2.9

@dfernandez79
Copy link

git config --global alias.opendiff difftool --no-prompt --tool opendiff --dir-diff

this did not work for me. just outputs the usage text.

Add quotes to the aliased command:

git config --global alias.opendiff "difftool --no-prompt --tool opendiff --dir-diff"

@nonopolarity
Copy link

can you have opendiff without installing the whole XCode app? (which is huge)

@bkeating
Copy link
Author

I don’t think running opendiff is possible without Xcode, unfortunately. See top answer on this thread: https://apple.stackexchange.com/questions/108025/is-it-possible-to-use-filemerge-as-standalone-app

@1oo7
Copy link

1oo7 commented May 4, 2020

Filemerge sucks. Pasting in lines of text it always deletes out all the whitespace.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment