109

My editor is changing the line endings of my source files. When I do git diff, I see the same line twice -- once with - and once with + -- with no visible difference.

How do I get git diff to show me what this change actually was?

ntc2
  • 11,203
  • 7
  • 53
  • 70
Stonky
  • 1,141
  • 2
  • 7
  • 6
  • 1
    Near duplicate of [this](https://stackoverflow.com/questions/5257553/coloring-white-space-in-git-diffs-output) (also mentioned below): my favorite is `git config diff.wsErrorHighlight all` – Joshua Goldberg Dec 05 '18 at 22:01
  • @JoshuaGoldberg - Indeed! That one has [the one and only comprehensive answer you need](https://stackoverflow.com/a/5259137/5110545). Simply don't bother with what others tell you below. – cueedee Apr 17 '23 at 12:17

5 Answers5

73

First, make sure you're using the coloured output (e.g. with git diff --color) and that you've enabled whitespace highlighting with (e.g.)

git config color.diff.whitespace "red reverse"

This might not work in all cases, however, as git doesn't appear to highlight trailing whitespace for removed lines. To see whitespace that you've deleted, simply use

git diff -R

to put the whitespace on the 'added' side of the comparison, where it does get highlighted.

For more detail, see the answers at this SO question.

Community
  • 1
  • 1
Paul Whittaker
  • 3,817
  • 3
  • 25
  • 20
  • I have git version 2.1.4, and I acted on the git config command suggestion. But git diff is still not showing any difference. If I pipe git diff to hexdump -C there is nothing at all showing there: `000000f0 2d 2d 7d 0a 2b 2b 7d 0a |--}.++}.|` The 7d is the '}' curly-brace. – cardiff space man Jan 21 '20 at 21:21
53

You can see line-ending difference with the following command.

git diff | cat -v

Then "^M" is printed for CRLF (DOS) ending, nothing for LF (Unix) ending.

Apparently git diff is doing the right thing, printing CR and LF characters for CRLF ending. But because CR is consumed by the console, we cannot see it. By using cat -v, we can make it visible.

Kaz
  • 686
  • 5
  • 11
  • another similar approach is `git diff > somediff && vi somediff`, vim shows CRLF as ^M as well. but the `cat -v` is neat. – Nick X Nov 28 '18 at 07:31
37

One way to see whitespace changes is to do a character-by-character "word diff" with

git diff --color --word-diff-regex=.

This highlights all whitespace changes everywhere in lines. Removed whitespace is wrapped in [- and -] and added whitespace in {+ and +}.

Alternatively, as suggested by Alex

git diff --color --ws-error-highlight=new,old

highlights all whitespace changes at the ends of lines.

Community
  • 1
  • 1
ntc2
  • 11,203
  • 7
  • 53
  • 70
20
git diff --ws-error-highlight=new,old

highlights whitespace diffs in changed lines.

Alex
  • 18,332
  • 10
  • 49
  • 53
  • What version of git are you using? git 2.1.4 on my system says --ws-error-highlight=new,old is an invalid option. – cardiff space man Jan 21 '20 at 21:24
  • This option was added in 2015 in git 2.5.0. – MiniGod Aug 25 '20 at 15:05
  • 1
    My git version `2.8.2.windows.1` shows the `^M` on the `+` lines but not on the `-` lines, which keeps making me think that my IDE (PhpStorm) is changing my line endings to DOS style, when in fact it's not. It's just preserving existing line endings of old files created by a bad editor. Your fix shows the line endings for both `-` and `+`, which is what git should be doing in the first place. – Buttle Butkus Mar 03 '21 at 00:01
5

A graphical diff tool will show you the change better -- try git difftool.

Use meld, and set the preferences to show whitespace. (Edit -> Preferences -> Show Whitespace.)

Other graphical tools probably have similar options -- @Cotton's answer+comment tells you how to do this with vimdiff.

bstpierre
  • 30,042
  • 15
  • 70
  • 103
  • A graphical diff tool may not be available. The answer by @paul-whittaker will work in the context implied by the question (some terminal). – beOn Jan 15 '18 at 23:37
  • Now this is weird, but `git difftool --tool=meld doesn't` even launch meld. I don't know if it is because a rebase is in progress or not. – cardiff space man Jan 21 '20 at 21:27
  • Meld does not show if the newline characters are LF or CRLF. – daniol Apr 06 '21 at 09:30