Git Diff External Tool Execution Flow: Meld and P4Merge


I explain the execution flow and how to configure for Git diff external tools and use Meld and P4Merge as my examples. The reason why I want to do this is because almost all the examples I found are always used without any explanation. They just throw out the whole bunch of things or steps and ask you to just follow them without actually knowing what exactly this specific step does or what exactly happened behind the scenes. I will explain all these things as best as I can here.

Meld and P4Merge Configure Steps

(1) To install Meld just run this command:

$ sudo apt-get install meld

To install P4Merge, just download the compressed file from  perforce website and extract to somewhere you want, let’s say the executable file p4merge hash the full path :


(2) A simpler way to configure is just wrote some shell script and specify this executable shell script in the .gitconfig file as git external diff tools. The detailed steps as well as their explanations are as follows:

2.a) write a script “” with the following content and save it into /usr/local/bin and add executable rights with chmod command.

meld "$2" "$5" > /dev/null 2>&1

2.b) modify ~/.gitconfig file and add the following lines:

external = /usr/local/bin/

The work flow: when you type in command git diff, the git will read the configure file and it sees “diff” is specified and it calls the external /usr/local/bin/ and pass 7 parameters to the script, the 2nd and 5th of which are the old-file and the new-file respectively. The scirpt accepts the 7 parameters and pass the 2nd “$2″ and “$5″ to meld (otherwise, it will report error). Here “> /dev/null 2>&1″ means we redirect standard error to standout and the standout of the meld output to /dev/null which is usually called “black hole” in linux, this basically means, we throw away all the output and error messages out of meld. 

(3) A more general and preferred configuration with P4Merge as an example is using two wrapper scripts. The detailed steps and the explanation are as follows:

3.a) create a shell script /usr/local/bin/ with the following content:

/home/yourname/p4merge.d/p4merge $*

3.b) create another shell script /usr/local/bin/ with the following content:

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

3.c) add executable rights to the two previous scripts and modify the ~/.gitconfig to add the following contents:

tool =
[mergetool ""]
cmd = \"$BASE\" \"$LOCAL\" \"$REMOTE\" \"$MERGED\"
trustExitCode = false
external =

Work follow: So next time you call git diff, it read the config file and notice the “diff” is specified with some external tools. Then it just runs the script and pass the “path old-file old-hex old-mode new-file new-hex new-mode” these 7 parameters to the script, as the content in indicates, it first compare the number of parameters $# to test if it is equal to 7 and if the parameter is correct, then it runs the other script and pass the two parameters $2 old-file and $5 new-file to the script. What does is just to call p4merge and pass p4merge with all the parameters it gets as in $*.


1. Git With Meld Diff Viewer on Ubuntu 2. Customizing Git – Git Configuratio 3. Special Shell Variables 4. Shell Comparison Operators


In this post, I just explained the execution flow for Git diff external tools configuration (Meld and P4Merge as examples) so that we know what exactly happened behind the scenes.

Written on October 5, 2014