Regarding code formatting I’m kind of purist :). I very often remove unnecessary white spaces (lines with only ws, ws at the end of lines etc). I even have set vim to show that kind of lines colored to red.
My problem is that using git-diff I often see something like this:
- else{
+ else{
Even if I have git-diff colored I can’t see difference (in that particular situation I removed 1 ws at the end of line). Is there any way to tell git-diff to show that ws colored to red? (for example those matched with /\s+$/ regexp).
With with Git 2.11 (Q4 2016) and after, you can do:
See doc on
git diffand ongit config.For versions older than that, you can set the
color.diff.whitespaceconfig setting, e.g. with:(I’m assuming that you already have
color.difforcolor.uiset toautosince you say that you see coloured patches fromgit diffanyway.)If you want to fine tune the type of whitespace errors that are highlighted in red, you can then change
core.whitespace, butblank-at-eolis 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 withgit 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
-Rkludge you can use the WhiteSpace Error Highlight option from the diff man page.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.