Git for Windows tip: Use P4Merge as mergetool

I recently found P4Merge (thank you Twitter and Git Immersion) and instantly dropped WinMerge as my standard diff/merge tool. I really like the way it visualises the differences and the 3-way merge is really nicely done. P4Merge is the merge tool for Perforce (which I have never used) and is both free and can be downloaded separately from the rest of Perforce.

The download can be found here. Choose Browse by component->Clients->Visual Merge Tool as you do not want to download the whole Perforce client package. In the installer for P4Merge you can choose which components you wish to install, you only need the Visual Merge Tool (P4Merge).

Install p4merge and then set it as your merge tool for git by running the following two config commands:

git config --global merge.tool p4merge
git config --global mergetool.p4merge.cmd 'p4merge.exe \"$BASE\" \"$LOCAL\" \"$REMOTE\" \"$MERGED\"'

EDIT: As pointed out by barik in the comments, in newer versions of git this works as well and is slightly simpler:

git config --global merge.tool p4merge
git config --global mergetool.p4merge.path "C:/Program Files/Perforce/p4merge.exe"

P4Merge can be used for doing diffs and merges. If using P4Merge for diffs then call:

git difftool

If the file you want to compare is already staged then use the –cached switch after that command.

When Git tells you that there has been conflict, to resolve it type:

git mergetool

This will open P4Merge and show three different versions of the file; your local version, the version you are trying to merge in (probably the master branch) and the base version. The base version is the common ancestor of the local version and the remote version. Choose which version wins or edit the merge manually and then save and quit P4Merge. Finally commit the merge and then remove any .orig files that may be left over. It should be possible to have git remove the .orig file automatically by setting mergetool.keepBackup to false in git config but I have not succeeded in getting that to work for me yet.

An example of using P4Merge

And this is what a 3-way merge looks like. The local version on the left, the base version in the middle and the remote version (the master branch) on the right. In the merge window at the bottom all three versions are currently selected. To select the local version, for example, I would click the blue diamond icon and then save and quit.

p4mergeCapture

13 thoughts on “Git for Windows tip: Use P4Merge as mergetool

  1. This is probably one of the better tutorials on using p4merge as the mergetool from within git. However, git now supports p4merge directly (to verify this, check the libexec/git-core/mergetools) folder, where the file p4merge is present.

    The two config commands should now be:

    git config --global merge.tool p4merge
    git config --global mergetool.p4merge.path "C:/Program Files/Perforce/p4merge.exe"
    
  2. Daniel, barik, thanks! Just chipping in my two cents because I had trouble with both of your instructions in setting up Git. What worked for me on Win 7 and XP at the git command prompt was:

    $ git config –global merge.tool p4merge
    $ git config –global mergetool.p4merge.cmd ‘p4merge $BASE $LOCAL $REMOTE $MERGED’

  3. Can anybody please send me to a step by step idiotproof to set winmerge or any other tool to my mergetool on git on Windows, I’ve followed every single insturction out there and I keep getting /usr/libexec/git-core/git-mergetool–lib: line 137: winmerge.exe: command not found and it is the same error for Meld, and p4merge

  4. looks like p4merge changed it so that the remote is now on the left while local is on the right. your article says local is on the left and remote on the right

Leave a comment