Using Perforce P4Merge As Diff Tool For Git

A friend of mine, Rizal Al-Mashoor introduced me to a tool named P4Merge by Perforce. One of the most annoying thing about Visual Studio 2013's built in diff tool is that it has no way (that I know if anyway) to ignore white spaces in code diffs. This can be exeedingly annoying when you are trying to merge code because white spaces are considered code changes as well.

Regarding the one that comes with the Git runtime for windows in the command line is good and usable but not the most convinent on the world when you want to have good visualization on your code changes.

Enough of my whining, lets get to it now. I took the liberty of going through the documentation and seeing how this can be done rather than Google or Stack Overflow. So this may already be covered but I will blog about this anyway.

This will be a step by steap guide on how to get this bad boy.

Note: This assumes you already have Git set up for command line access.

Step 1

Download P4Merge from the Perforce web site.

Step 2

Install P4Merge. You will need to disable some options because at least for the purposes of this blog post, we are not using perforce products. Your module selection screen should look something akin to this (we are only interested in the Visual Merge Tool):

P4 Merge Component Selection Window

Step 3

Run the following commands in your command prompt/powershell terminal:

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

Okay, 3 being a charm, thats all you need for basic integration between Git and P4Merge. Now you can run diffs and merges between files via the following commands:

git difftool
git mergetool

While I am roughly familliar on how to do it, I usually prefer to use the SourceTree for this sort of thing.

Simply use SourceTree to select the revision/commit/push number you are interested in, select the filw you want and then do the following:

Source Tree Revision Browser

Since now we have used the command line tool to specify this as the global diff and merge tool, SourceTree will use P4Merge as the diff/merge tool. However, a word of caution, SourceTree does come shipped with its own embedded Git client. If that is the case, you will either need to configure the Git instance that comes embedded with SourceTree or use the global one. To check which one is SourceTree using, go here:

Click Tools->Options->Git->System Git

Source Tree Git Settings

Hope this helps.