How to Get GitHub-like Diff Support in Git on the Command-Line
If you use Git as your VCS (version control system), you’ll know that it has excellent diff support on the command line. However, even as good as it is, it still leaves a bit to be desired — at least when compared with tools such as GitHub.
To put it into a bit better context, whether I’m developing software or writing technical documentation, or writing blog posts, I’m almost constantly using Git. This can be for any number of things, but most commonly it’s for reviewing changes.
As the documentation lead at ownCloud, one of the key tasks is reviewing PRs created in the documentation repository. This isn’t a hard thing to do, and thanks to some Git aliases is a breeze from the command-line.
However, recently I started to become a bit envious of the diff support that GitHub’s web interface offers. Take the screenshot above. You can see that there are two sets of additions. Something that you’d commonly see when reviewing a diff of any nature.
What’s important though, is that you can see the exact changes. Doing so makes it much easier to make an informed decision about whether to accept or reject the change, or whether to request further changes.
Compare that to the same diff in Git in the macOS terminal, which you can see above. Besides the fact that the additions and removals are listed underneath each other, instead of side-by-side, you have very broad granularity concerning what changed. All you can see is what was there in the previous version, and what’s been changed.
Given that, it occurred to me that if GitHub can do it, then I should be able to as well. After a little bit of thought, I remembered reading, some time ago, about a much richer form of diff in Git 2.9.
Given that I have version 2.10 installed, I did a bit of googling and found the article that I’d previously read. In the article, under the section header "Beautiful diffs", I found a key bit of information about the latest diff support:
You probably already know that Git can colorize its diff output, and you can even customize the colors yourself. However, there are also scripts that filter Git's output and change it even further. For instance, there's a diff-highlight script that puts an extra emphasis on the changed part of a line.
When I read that and saw the provided screenshot, I thought I was in nerd heaven. I need no longer have GitHub envy. I could have just as fine a level of diff support using the command-line. I didn’t have to use another tool!
And you don’t either, if you’re using Git from the command-line. Sound like something that you want? Great.
The catch is, you may need to install it.
If you’re using a Linux distribution, use your native package manager to make sure that you’re at least using Git 2.9 (ideally higher).
which diff-highlight to find the script’s location.
If you’re using macOS, I recommend using HomeBrew to upgrade to the latest version of Git.
After that, diff-highlight should be located at
/usr/local/Cellar/git/<your GIT version>/share/git-core/contrib/diff-highlight/diff-highlight, changing
<your GIT version> for your version of Git.
Regardless of how you install it after you have, copy the script to
That way, it will always be available to your account, as well as to anyone else, if there are other users.
Ok, you don’t strictly need to.
It’s just a convenience that I usually make use of.
With that done, we just need to make one more change.
~/.gitconfig, make sure that the
core section looks at least like the following:
[core] pager = /usr/local/bin/diff-highlight | less
Then, you’re ready to use it and get some Git diff command-line goodness, which you can see below.
And that’s how to almost match the visual beauty and granularity of GitHub’s diff outputs. While diff-highlight isn’t, exactly, the same, it is virtually so. For a tool that’s been around for some time now, I’m surprised that it’s not enabled by default. I’m sure there are reasons for that, ones which I’ve not done any research to uncover.
And just one more thing. While I still don’t have side-by-side diffs, I’m planning to play more with difftool to get that. Sure, I could just use GitHub, SourceTree, SmartGit, or one of the plethora of other Git tools. But I just love the command-line. If you do as well, and haven’t used diff-highlight yet, give it a try. It’s well worth it.
Don’t miss my next post. Drop your email in the box below, and get it straight to your inbox, PLUS exclusive content only available by email. No spam, and you can unsubscribe at any time.