Colour Diffs for Everything

I diff a lot of text files while I’m working and for the most part I’m usually comparing files under version control, but that’s not always the case. Reading diffs in a format I’m used to makes things considerably easier to understand, so I need a simple way to generate unified diffs with colour between arbitrary files.

tldr; use Git to diff everything with git diff --no-index <path> <path>.

Oldschool

For the longest time I used, but was unimpressed with, the basic diff command. If I wanted to get the difference between two files I would execute diff <path> <path>. This outputs “Context Format” diffs which look something like this:

1,2c1,4
< First Line
< Second Line
---
> New First Line
>
>
> Forth Line

While this does convey the necessary information I find it much less intuitive when compared to the “Unified Format”, which most other tools I use output by default:

--- a.txt	2017-03-04 13:25:53.000000000 -0500
+++ b.txt	2017-03-04 13:26:37.000000000 -0500
@@ -1,2 +1,4 @@
-First Line
-Second Line
+New First Line
+
+
+Forth Line

Eventually I decided to RTFM and found I could generate unified diffs by passing the -u or --unified flag to diff.

Something’s Missing

Unified diffs are a huge step up from context diffs but output from diff lacks any colour, something I find helps tremendously with readability. Searching the internet for a way to colourize the output of diff led to me colordiff. While this works nicely it doesn’t seem to come installed by default on systems I use.

I can however, pretty much guarantee Git will be installed on any random machine I need to access.

Just Use Git

Git has a handy feature that allows you to use git diff for arbitrary files, simply by passing the --no-index flag.

$ git diff --no-index <path> <path>

Using this you can generate a diff for any two files that will display in the same format you’ve configured for Git. I use a bash alias gdiff for easy access:

alias gdiff='git diff --no-index'

Contents