202

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).

radarek
  • 2,478
  • 2
  • 17
  • 12
  • 5
    If you invert the colors (swap foreground and background), whitespace changes like this will show up. An easy way to accomplish this in many terminals is to highlight the text in question with the mouse. This trick only works with a colored diff, of course. – Phlarx Jan 20 '17 at 21:37
  • Use `git config diff.wsErrorHighlight all`, as the accepted answer now explains at the top. – sideshowbarker Feb 21 '22 at 04:42
  • or if you don't want to permanently change your config (or like one-liners): `git -c diff.wsErrorHighlight="all" diff ` – Cole Aug 16 '23 at 07:27

6 Answers6

229

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.

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
Mark Longair
  • 446,582
  • 72
  • 411
  • 327
  • 34
    "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." Exactly! And there is no way to show it also for removed lines? (hey, it's diff :)) – radarek Mar 10 '11 at 15:38
  • 1
    Not as far as i know, but I could very easily have missed something. I guess the logic is that since you're taking those lines out, who cares whether they had whitespace errors or not? :) – Mark Longair Mar 10 '11 at 15:46
  • 6
    Add --global to set in your ~/.gitconfig – simlmx Jul 25 '13 at 15:31
  • 17
    @radarek: you can use the reverse option: `git diff -R` – blueyed Nov 13 '13 at 21:52
  • 6
    Is there a bug report for this? If not, it seems like there should be. – Ajedi32 Jul 01 '14 at 19:18
  • This is great! Thanks. Is there a way to setup the git config so `git diff` will always run as if `-R` was being added in the terminal? – f1lt3r Mar 16 '16 at 14:54
  • 1
    I'd find that *very* confusing, personally! There's no such config option, I think, but you could create an alias to do it, e.g. `git config --global alias.diffr 'diff -R'` so then you can use `git diffr` for the reversed diff. – Mark Longair Mar 16 '16 at 21:39
  • @Ajedi32 Solution is to use `git diff --ws-error-highlight=new,old` – Michaelangel007 Aug 10 '16 at 14:27
  • 5
    this worked `git config diff.wsErrorHighlight all`. Use `git config --global [...]` to make the changes global (ie affecting all repos). – Felipe Alvarez Dec 28 '18 at 05:52
  • 1
    I would have lead with "git config --global diff.wsErrorHighlight all". – null Nov 21 '19 at 00:03
  • Is there a way to show them also when using `git add -p`? – KumZ Dec 08 '20 at 21:34
  • for those of you who like oneliners: `git -c diff.wsErrorHighlight="all" diff ` – Cole Aug 16 '23 at 07:25
162

Use git diff -R to turn removed lines into added lines. Then trailing whitespace will be highlighted.

(This assumes you already have whitespace hightlighting enabled, as per the colour settings from Mark's answer. Credit for this method goes to Junio's post at http://git.661346.n2.nabble.com/Highlighting-whitespace-on-removal-with-git-diff-td5653205.html .)

For example, when converting a file from DOS line endings to Unix, git diff -R clearly shows me the ^M characters (dis)appearing at the ends of lines. Without -R (and also without -w etc.) it shows that the entire file has changed, but doesn't show how.

Paul Whittaker
  • 3,817
  • 3
  • 25
  • 20
  • 5
    Of course, you can also do `git diff | cat -A | less -S` if you're desperate, but in addition to the carriage returns, the `cat` will also display any colour highlighting escape codes literally. – Paul Whittaker Jul 16 '12 at 17:30
  • 3
    @Paul_Whittaker `cat -A` isn't portable. On BSD cat, there is no such option. Please use `cat -vet` instead. – 7heo.tk Apr 25 '15 at 08:07
12

For the lazy answer skimmers, just run:

git config --global diff.wsErrorHighlight all

Then git diff will highlight trailing whitespaces in removed lines as well.

user1338062
  • 11,939
  • 3
  • 73
  • 67
11

Use git diff --color | less -R. The -R makes the color control codes human-friendly.

Then you can use less's regular expression search, e.g.

/[[:space:]]+$
Kelvin
  • 20,119
  • 3
  • 60
  • 68
0

My version of git diff already seems to do this - I have git 1.7.4.1 and have set color.ui = auto.

nickgrim
  • 5,387
  • 1
  • 22
  • 28
  • 13
    I just tested with git 1.7.5.1 and it certainly does not highlight trailing whitespace in lines being removed. – Infiltrator May 23 '11 at 23:39
0

The command mentioned above can also be included in the gitconfig file as options, i.e instead of

git config ....

adding the corresponding options to e.g ~/.gitconfig

[diff]
    wsErrorHighlight = all

[color]
    ui = auto

the two options above should do the trick. The earliest git version I tested this was 1.7 but should work for any version after that too. On recent versions of git the "[color]" options is set to "auto" by default.

mher
  • 369
  • 3
  • 7