Commandline git diff that highlights exactly what is changed

Inpired by this great post on how to use the meld diff tool to get a nice GUI view of git diffs, I started looking for a way to get a similarly good result on the command line. That is, a diff that does not just show the whole lines that are changed, but that highlights the actually changed parts of each lineHere I document what I found. 

It turned out I could even get something better, than meld, in that I can view both the removed and added part in the same view (color coded with red/green) which my brain find much easier to interpret.

So, to the steps.

First I specify that git should use some external tool for it's diffs, by editing my ~/.gitconfig file and adds this to the bottom:

    external = git-wdiff
Then, I go on and create that tool, "git-wdiff", in /usr/local/bin/git-wdiff, and put this text into it:

wdiff -n $2 $5|colordiff|sed -r 's/(\{\+|\+\}|\[\-|\-\])//'|less
You'll need to install wdiff and colordiff (available in ubuntu repos).

What it does is:

  • wdiff produces a syntax where each deleted chunk of word(s) is surrounded with "[-" and "-]", and each added chunk with "{+" and "+}". (the "-n" flag is to prevent diff chunks to span over newlines, which will cause problems to colordiff).
  • colordiff gives nice red/green (depending on your terminal color sheme) coloring of this output
  • the sed part removes those [- ]- and {+ +} parts, so that you only have the color coding left.
  • less just adds a nice pager, for easy scrolling.
  • See the results below:

EDIT: [ Egon Willighagen] hinted me at another way to do this, where you don't even need colordiff, but can do the coloring directly with wdiff, by using it's facility for adding arbitrary start and end sequences around the changed words. Then the command in /bin/git-wdiff would become:

diff -u $2 $5 | wdiff -n -d -w $'\033[30;31m' -x $'\033[0m' -y $'\033[30;32m' -z $'\033[0m' | less
The codes for the colors might need some tweaking to fit your terminal's color scheme. (More info about that [ here] ).


Status: Git on windows

Since I'm originally a windows user and still using it sometimes, I'm keeping an eye on the status of git on windows.

Seems like there's more to do, but some things are happening: MSysGit Seems to be the currently best solution, while GitSharp looks promising. For MSysGit there's a guide on getting started with Git and GitHub on Windows.


Must-have git how tos

Realized I would need to go through a git tutorial containing the basics.

I liked this one. Very concise, and has sections for the typical roles you might have as a git user. I liked this one too. A good complement to the other.

How to check out a specific branch from github

I ran into troubles trying to check out a specific branch from github.

I asked about it at #git on FreeNode IRC channel. I'll summarize how to do it:

First run:
git clone git://[path to your project]...git

Then go:
git branch -a see what branches you have.


Had to learn some basic vim

I just realized that git is using vim as text editor (hope it is possible to change to nano or equivalent). Anyway, as for now I guess I better just learn som basic vim commands. saved me.

The most important commands to remember are:

i - enter "insert mode"
a - enter "append mode"
ESC - leave "insert" or "append mode"
x - delete one character
dd - delete one line (and store it in clipboard)
ZZ - Save and close
ZQ - Close w/o saving


Looking into git

I found a git tutorial at Looks useful for svn users, since it has the paired the corresponding git / svn commands. Another one is as well.