While there are some great diff tools out there, there are very few good merge tools. My favorite diff tool is Kaleidoscope. And there are several others I wouldn’t mind using.

However, Kaleidoscope does not do merging. Too me, software development requires much more merging then diff’ing. And many times, github.com’s HTML diff view is more than sufficient. Heck, even command line diff works just fine for small differences. Looking at diff’s is easy and paying money for a tool that only does diffs is something I find hard to swallow.

Taking three files, and coming up with a new one is quite a bit more difficult. In my experience, not many tools handle it well. The biggest problems are:

  • Integration and interoperability with the OS (cough, OS X native)
  • Intelligent resolution of conflicts
  • Differences within a line
  • Easily choosing which diff chunks to take
  • Being able to edit the final merged file

Many times, the merge tool will not be fine grained enough to allow for differences within a line to be accurately merged. You’ll even have problems with whole lines of code not being intelligently merged.

The two best tools I’ve found to handle this are:

Araxis Merge was originally a windows product that I used in a previous life. It was great because it offered not just merging, but standard diffs, as well as directory diffs. Really nice and I highly recommend. However, I don’t use it anymore, even though they have a Mac version. The problem is the price is absurd: $269 and then $49 a year, thereafter. That’s insane.

Especially insane, when the next best tool is free! P4Merge is actually from another SCM company, Perforce. It is more of a single purpose tool then something like Araxis Merge. However, it does do merging very well. I’ve found that it can start to get confused on resolving conflicts, such as when an existing chunk of code is just moved down the file while another commit inserted new code at the same spot. But that’s where being able to edit the final merged file at the same time helps.

P4Merge

(Notice the yellow, blue, and green icons on the right side of the bottom pane. You click each one to switch which code is used in the final output.)

You would think that using P4Merge with a competing SCM like git would not be easy. But it’s actually quite straightforward. Go install P4Merge and then just add this to ~/.gitconfig:

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

When you need to merge with git, run

git mergetool

Instead of the normal, git merge. The one problem I’ve run into is that even though keepTemoraries/keepBackup is false, I still get .orig files littered about when a merge happens. These have to be cleaned up manually.

UPDATE (5/30/12): I was able to fix the .orig files being left by using a shell script:

#!/bin/sh

# Switched according to: http://www.andymcintosh.com/?p=33#comment-94174
#/Applications/p4merge.app/Contents/MacOS/p4merge $*

for arg; do [[ $arg = /* ]] || arg=$PWD/$arg; absargs+=("$arg"); done;
/Applications/p4merge.app/Contents/Resources/launchp4merge "${absargs[@]}"

Put it in /usr/local/bin and name it p4merge. My new .gitconfig section for merge tool looks like:

[mergetool "p4merge"]
    cmd = p4merge $BASE $LOCAL $REMOTE $MERGED
    trustExitCode = false
    keepBackup = false
    prompt = false
About the Author:

Learned something? Great! Need help on your development project? I'm available for hire:

  • Ruby on Rails
  • iOS Development
  • System Architecture & Performance

Get in touch:

Discussion

  1. david8401 says:

    Mike, thanks for the post about p4merge.
    However I was not able to resolve the .orig files issue. Does the fix above really work for you?

  2. Mindy Engelberg says:

    I had the same .orig file not going away problem. I fixed it by setting the keepBackup config like this:

    [mergetool]
    keepBackup = false

    or

    git config –add mergetool.keepBackup false

    Setting it on the mergetool p4merge section did not work.

    BTW, this is on Windows.

  3. If you’re on Mac OS, why aren’t you using FileMerge (opendiff)? It comes with the developer tools, integrates with Git perfectly, and is better than anything else I’ve tried including p4merge.

Leave a Comment