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.

bash
$ 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.

bash
$ 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.

bash
$ 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.

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

Installation

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:

bash
$ curl https://raw.githubusercontent.com/git/git/master/contrib/diff-highlight/diff-highlight > ~/bin/diff-highlight && chmod +x ~/bin/diff-highlight

Configuration

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.

ini
[core]
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.

ini
[alias]
worddiff = !git diff --color | diff-highlight | less -r