diff small changes (and long lines!) by character

4. August 2020

I recently had to work with some test fixtures containing extremely long lines. Using a traditional line-based diff on these to ensure only the smallest changes necessary were made and committed was difficult, and so I delved into the depths of the git-diff man page to see if there was a better way.

Combining two options, --word-diff-regex and --word-diff, it’s possible to see changes inline, side-by-side. For example, changing AaA to BbB will show as ABabAB.

--word-diff-regex allows you to specify a regular expression to determine what a word is. This specific use case is even called out on the manpage, which notes: --word-diff-regex=. will treat each character as a word and, correspondingly, show differences character by character.

--word-diff forces the diff to be shown using only colors, without any +, -, or bracket characters demarcating changes.

Hopefully this saves you some squinting.