GitHub-style diff in command line git

GitHub has a really nice feature that emphasises changed words in a changed line:

Old vs New Diff word highlighting

Here’s how to get it in command line git.

Word diff in command line git

I’ve installed git on Mac OS X through Homebrew. If you’ve installed it some other way, or you’re on a different system you’ll need to find where git-contrib is installed.

git-contrib includes diff-highlight – exactly what we’re after.

Just add the following to your ~/.gitconfig:

[pager]
  diff = /usr/local/Library/LinkedKegs/git/share/git-core/contrib/diff-highlight/diff-highlight | less
  log  = /usr/local/Library/LinkedKegs/git/share/git-core/contrib/diff-highlight/diff-highlight | less
  show = /usr/local/Library/LinkedKegs/git/share/git-core/contrib/diff-highlight/diff-highlight | less

Note that I’ve used /usr/local/Library/LinkedKegs/git to link to the latest version of a Homebrew formula. This will ensure diff-highlight is the one included with the current version of git.