59

I have the following output with git-diff.

- // sort list based on value    
+ // sort list based on value

How can I see easily see the number of removed tabs/spaces at the end of the line ?

3
  • 1
    I would rather tell git to report space/tabs with the .gitconfig option [alias] df = diff --ignore-space-at-eol -b -w --ignore-blank-lines. This is not an answer but possible workaround...
    – nowox
    Aug 28, 2014 at 14:00
  • @coin That does not help, it get rid of the spaces and tabs entirely and is no longer visible in the diff :-(
    – Nico
    Aug 28, 2014 at 14:03
  • 8
    git diff --ws-error-highlight=all can also be useful (with Git 2.5+, Q2 2015). See my answer below
    – VonC
    Jun 12, 2015 at 13:12

2 Answers 2

52

Note: Git 2.5+ (Q2 2015) will propose a more specific option for whitespace detection.

See commits 0e383e1, 0ad782f, and d55ef3e [26 May 2015] by Junio C Hamano (gitster).
(Merged by Junio in commit 709cd91, 11 Jun 2015)

diff.c: --ws-error-highlight=<kind> option

Traditionally, we only cared about whitespace breakages introduced in new lines.
Some people want to paint whitespace breakages on old lines, too. When they see a whitespace breakage on a new line, they can spot the same kind of whitespace breakage on the corresponding old line and want to say "Ah, those breakages are there but they were inherited from the original, so let's not touch them for now."

Introduce --ws-error-highlight=<kind> option, that lets them pass a comma separated list of old, new, and context to specify what lines to highlight whitespace errors on.

The documentation now includes:

--ws-error-highlight=<kind>

Highlight whitespace errors on lines specified by <kind> in the color specified by color.diff.whitespace.
<kind> 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.

For instance, the old commit had one whitespace error (bbb), but you can focus on the new errors only:

old and new shitespace errors

(test done after t/t4015-diff-whitespace.sh)


Update Git 2.11+ (Q4 2016, a year and half later) :

git config diff.wsErrorHighlight [old,new,context]

git diff/log --ws-error-highlight=<kind> lacked the corresponding configuration variable to set it by default. That is added in Git 2.11.

See commit 0b4b42e, commit 077965f, commit f3f5c7f (04 Oct 2016) by Junio C Hamano (gitster).
(Merged by Junio C Hamano -- gitster -- in commit e5272d3, 26 Oct 2016)

2
  • 2
    does the syntax git config diff.wsErrorHighlight all work with 2.11 as well? Dec 6, 2016 at 1:14
  • 3
    @GrantHumphries I just tested it: yes it does (git version 2.11.0.windows.1)
    – VonC
    Dec 6, 2016 at 5:22
22

I can think of multiple options:

  • Configure Git to use colors: git config --global color.ui true. Whitespace at the end of lines is now highlighted in red.

  • Pipe the output of git diff through cat: git diff | cat -A. The -A flag tells cat to show non-printable characters (e.g. ^I for tab).

5
  • The cat works but I'd prefer a git only solution with config. Setting the ui.config to true does not change anything to my output. I found that git config color.diff.whitespace "blue reverse" works for space but not for tabs (there's no color.diff.tabs config).
    – Nico
    Aug 28, 2014 at 14:16
  • @Nico: woops, that was meant to be color.ui, not ui.config :)
    – knittl
    Aug 28, 2014 at 14:18
  • 3
    On OS X (BSD?) the options are -v for displaying non-printing characters and -e shows a dollar sign at the end of each line.
    – Carl G
    Jan 28, 2015 at 18:22
  • I think you want -t for mac instead of -v. This displays tabs as ^I
    – DylanYoung
    Jan 8, 2019 at 14:48
  • The git config didn't seem to work when removing a trailing whitespace but the cat did, thanks!
    – rogerdpack
    Oct 20, 2022 at 6:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.