Using p4merge as a git mergetool

So, don’t get me wrong, I hate Perforce. It actually makes my head hurt a little remembering my experience with it. The only saving grace was the Perforce merge tool, p4merge. There are number of different diff/merge tools that will work with git on OS X, but I’m not all that impressed with them — even the rather expensive Changes left me feeling underwhelmed.

Perforce Visual Merge Tool

Perforce Visual Merge Tool (shown on Windows)

After figuring out how to configure git to use Changes as its merging and diff tool, I set out on a mission (read: Google search) to figure out how to use p4merge instead. It turns out it’s pretty simple, but that all the pages with instructions on how to do it no longer exist.

Here are the steps to get it working:

Download and Install P4V

Download the free Perforce Visual Client dmg from here. Once it’s downloaded, copy p4merge from the disk image to your /Applications directory.

Write some simple shell scripts

p4merge*

Create a new text file in /usr/local/bin called p4merge and add the following lines:

#!/bin/sh
/Applications/p4merge.app/Contents/MacOS/p4merge $*

Make the script executable by entering this command:

chmod +x p4merge

p4diff*

Create a new text file in /usr/local/bin called p4diff and add the following lines:

#!/bin/sh
[ $# -eq 7 ] && /usr/local/bin/p4merge "$2" "$5"

Make the script executable by entering this command:

chmod +x p4diff

Configure Git to use the scripts

Open your git configuration file (probably ~/.gitconig) and add these lines:

[merge]
	keepBackup = false;
	tool = p4merge
[mergetool "p4merge"]
	cmd = p4merge "$BASE" "$LOCAL" "$REMOTE" "$MERGED"
	keepTemporaries = false
	trustExitCode = false
	keepBackup = false
[diff]
	external = p4diff

Use it!

That’s it. Now when you run git-mergetool or git-diff the visual Perforce merge tool will launch with the files you want to merge or diff. Hope this helps!

This entry was posted in git. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

10 Comments

  1. Posted April 5, 2009 at 8:40 am | Permalink

    I am definitely going to use this. Thanks for the steps!

  2. Posted April 5, 2009 at 8:54 am | Permalink

    You’re my hero, Mr. McIntosh

  3. Posted April 12, 2009 at 9:56 am | Permalink

    make sure that you change the && to an actual &&

  4. Posted April 12, 2009 at 9:57 am | Permalink

    and by && I mean & &

  5. Posted May 5, 2009 at 10:30 pm | Permalink

    This doesn’t seem to work when there is only a LOCAL and REMOTE, but no BASE… p4merge gets hung up looking for the BASE, which does not exist.

  6. Senthil A Kumar
    Posted May 12, 2009 at 11:31 pm | Permalink

    Thanks a lot for the post, but

    1)
    #!/bin/sh
    [ $# -eq 7 ] && /usr/local/bin/p4merge “$2″ “$5″

    2)
    cmd = p4merge “$BASE” “$LOCAL” “$REMOTE” “$MERGED”

    But what does the above line do? Can i use the same thing for GIT on UNIX?
    Someone please help

  7. Posted July 2, 2009 at 2:18 pm | Permalink

    You can save yourself from having to create the script if you use the launchp4merge that comes bundled:

    [mergetool "p4merge"]
    cmd = /Applications/p4merge.app/Contents/Resources/launchp4merge “$PWD/$BASE” “$PWD/$REMOTE” “$PWD/$LOCAL” “$PWD/$MERGED”
    keepTemporaries = false
    trustExitCode = false
    keepBackup = false

    With this, all you have to do is install the app.

  8. Ben
    Posted August 25, 2009 at 11:39 am | Permalink

    when I use this, it will open a different window for each file in to diff. how do I open it all in 1 p4merge process? I figured this out for merging by googling around, but i’m not sure how to do this for diffing.

  9. Luís Miranda
    Posted September 23, 2009 at 8:52 am | Permalink

    Short and sweet, thanks!

  10. SooTyLaD
    Posted January 8, 2010 at 8:59 am | Permalink

    I have 3 questions regarding this if you don’t mind?

    1) After I “Hit return to start merge resolution tool (p4merge)” it pops straight up with this:

    2010-01-08 15:50:36.269 p4merge.real[58490:80b] Can’t open input server /Library/InputManagers/Inquisitor

    Any idea what this is and whats wrong? the editor still starts and all seems well but things like this bug me :p

    2) despite having trustExitCode set to false, it doesn’t ask me if the merge is complete like it would with say vimdiff?

    3) i have used both sets of keepBackup settings (both set to false, in fact, everything is as above) but if still leaves pesky .orig files in my working directory… any ideas?

    I have a feeling these are linked, could anyone shed some light? Also quite interested to see the authors answer to W. Andrew Loe III’s post?

One Trackback

  1. By Sönke Rohde » GIT Tips and Tools on November 5, 2009 at 8:27 am

    [...] for merging. I am not a big Perforce fan but the free p4merge tool is really great. Check out the blog post by Andy McIntosh for a detailed explanation how to set it [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">