How can I visualize per-character differences in a unified diff file? How can I visualize per-character differences in a unified diff file? git git

How can I visualize per-character differences in a unified diff file?


In git, you can merge without committing. Merge your patch first, then do:

git diff --word-diff-regex=.

Note the dot after the equals sign.


Here are some versions with less noisy output than git diff --word-diff-regex=<re> and that require less typing than, but are equivalent to, git diff --color-words --word-diff-regex=<re>.

Simple (does highlight space changes):

git diff --color-words

Simple (highlights individual character changes; does not highlight space changes):

git diff --color-words=.

More complex (does highlight space changes):

git diff --color-words='[^[:space:]]|([[:alnum:]]|UTF_8_GUARD)+'

In general:

git diff --color-words=<re>

where <re> is a regexp defining "words" for the purpose of identifying changes.

These are less noisy in that they color the changed "words", whereas using just --word-diff-regex=<re> surrounds matched "words" with colored -/+ markers.


git diff --color-words="[^[:space:]]|([[:alnum:]]|UTF_8_GUARD)+"

The above regex (from Thomas Rast) does a decent job of separating diff fragments at the punctuation/character level (while not being as noisy as --word-diff-regex=.).

I posted a screenshot of the resulting output here.


Update:

This article has some great suggestions. Specifically, the contrib/ tree of the git repo has a diff-highlight perl script that shows fine-grained highlights.

Quick start to use it:

$ curl https://git.kernel.org/cgit/git/git.git/plain/contrib/diff-highlight/diff-highlight > diff-highlight$ chmod u+x diff-highlight$ git diff --color=always HEAD~10 | diff-highlight | less -R