Coloring white space in git-diff’s output

With with Git 2.11 (Q4 2016) and after, you can do:

git config diff.wsErrorHighlight all

See doc on git diff and on git config.


For versions older than that, you can set the color.diff.whitespace config setting, e.g. with:

git config color.diff.whitespace "red reverse"

(I’m assuming that you already have color.diff or color.ui set to auto since you say that you see coloured patches from git diff anyway.)

If you want to fine tune the type of whitespace errors that are highlighted in red, you can then change core.whitespace, but blank-at-eol is enabled by default so you probably won’t need to change that for the example you mention.

A possible source of confusion is that in the output of git diff, whitespace errors are only highlighted in the lines that are introduced, not those that are removed. (Update: as Paul Whittaker points out in his answer, which you should up-vote :), you can see these by reversing the sense of the diff with git diff -R.)

You can find more documentation on these config options in the git config man page

If you don’t want to use the -R kludge you can use the WhiteSpace Error Highlight option from the diff man page.

–ws-error-highlight=

Highlight whitespace errors on lines specified by in the color specified by color.diff.whitespace.
is a comma
separated list of old, new, context. When this option is not given,
only whitespace errors in new lines are highlighted. E.g.
–ws-error-highlight=new,old highlights whitespace errors on both deleted and added lines. all can be used as a short-hand for
old,new,context.

git diff --ws-error-highlight=new,old <file>

or

git diff --ws-error-highlight=all <file>

With versions older than 2.11, there’s no way to permanently turn this on and store this in config aside from using an alias:

git config alias.df 'diff --ws-error-highlight=all'

Now you can use:

git df <file>

To see the changes in red.

Leave a Comment