How to Get GitHub-like Diff Support in Git on the Command-Line

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.

GitHub’s Diff

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.

Git’s standard diff output

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). Then run 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 /usr/local/bin. 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. In ~/.gitconfig, make sure that the core section looks at least like the following:

    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.

Git’s diff output, using diff-highlight as the pager

In Conclusion

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.

You might also be interested in these tutorials too...

Mon, Oct 16, 2017

Easy Git Interaction with OhMyZsh and Git Extras

Git is known to be incredibly terse, though it has improved in recent years. What’s more, there’s a plethora of commands that you need to remember. So, why not learn about OhMyZsh and Git Extras and make your life with Git easier!

Fix a Commit History With Git Interactive Rebase
Thu, Nov 28, 2019

Fix a Commit History With Git Interactive Rebase

Git interactive rebase is a powerful tool that can help you fix up your commit history to make it more meaningful, professional, and maintainable. In this post, I step through what it is, how it works, and when you should — and shouldn’t — use it.

4 Git Command-Line Tips for Greater Productivity
Thu, May 23, 2019

4 Git Command-Line Tips for Greater Productivity

Git, despite still being a bit terse, is extremely powerful version control software. However, because it’s so powerful, it takes time to learn. In this post, I’m going to show you four small techniques to help you use it more effectively.

Want more tutorials like this?

If so, enter your email address in the field below and click subscribe.

You can unsubscribe at any time by clicking the link in the footer of the emails you'll receive. Here's my privacy policy, if you'd like to know more. I use Mailchimp to send emails. You can learn more about their privacy practices here.

Join the discussion

comments powered by Disqus