git and diff-highlight

03 Mar 2015 in Tech

diff-highlight is a contrib script that ships with git. It's a better way to visualise a diff when the changes are small words, not entire lines/paragraphs. It's hard to explain, so here's an example (diff-highlight is the script):

Standard git diff

This is git diff as we know it.

$ git diff
Git diff output

Add diff-highlight

Let's add diff-highlight. This makes it easier to see the changes, but we've lost our colours. This is because git disables colours when output is piped to another command.

$ git diff | diff-highlight
Git diff output with diff-highlight

diff-highlight and colours

To get the colours back, we add the --color flag to git diff to force colour output.

$ git diff --color | diff-highlight
Git diff output with diff-highlight and colour

Large diffs

If you're looking at quite a large diff, you might want to pipe it into less. By default, less doesn't support colours. Fortunately this is easy to enable by adding the -r flag.

$ git diff --color | diff-highlight | less -r


To install diff-highlight, you'll need to get a copy of the script and put it somewhere in your $PATH. You'll also need to make the script executable.

If you have ~/bin in your path, here's a one liner to install:

$ curl > ~/bin/diff-highlight && chmod +x ~/bin/diff-highlight


If you like this behaviour and want to use it frequently, it may be worth configuring your git installation for easy access. All of these changes are made in ~/.gitconfig.

The easiest way to do it is to enable it for everything by changing your pager setting so that all output is piped through diff-highlight before being passed to your pager. This is what it looks like for me.

pager = diff-highlight | less -r

If you'd rather just enable it for certain actions, it's easy to add as an alias. I've added the following to my .gitconfig and use it with git worddiff.

worddiff = !git diff --color | diff-highlight | less -r