476

I love to use git diff -w to ignore whitespace differences. But, I just noticed that it ignores even whitespace differences in the middle of lines. How could I only ignore whitespace differences that come at the start (^) or end ($) of lines?

ma11hew28
  • 121,420
  • 116
  • 450
  • 651
  • 51
    Considered using `git diff -b` instead? – Jonas Byström Nov 08 '13 at 09:18
  • 12
    "-b --ignore-space-change Ignore changes in amount of whitespace. This ignores whitespace at line end, and considers all other sequences of one or more whitespace characters to be equivalent." – mowwwalker May 31 '18 at 00:49
  • 7
    For those who don't know, `git diff -b` is another name for `git diff --ignore-space-change` – aafulei Dec 04 '20 at 02:28
  • There are a number of related but not identical questions. What i want is 'can i be sure my change doesn't change functionality'. Which of following spaces meet this definition " puts 'hello world ' ". – justintime Aug 02 '21 at 09:25
  • @justintime I don't think Git (or any other tool that I know of) can do what you want. Maybe try writing tests or using a linter, or both. – ma11hew28 Aug 04 '21 at 17:40
  • Can I just turn on -w permanently? Like I would ever care about whitespace changes. – Gerry Jun 10 '22 at 16:57
  • @Gerry I don't think so, but you could create a [Git alias](https://git-scm.com/book/en/v2/Git-Basics-Git-Aliases), eg, by running the command `git config --global alias.df 'diff -w'`. Then, instead of typing `git diff -w`, you could just type `git df`. – ma11hew28 Jun 10 '22 at 21:21

2 Answers2

560

For end of line use:

git diff --ignore-space-at-eol

Instead of what are you using currently:

git diff -w (--ignore-all-space)

For start of line... you are out of luck if you want a built in solution.

However, if you don't mind getting your hands dirty there's a rather old patch floating out there somewhere that adds support for "--ignore-space-at-sol".

Fake Code Monkey Rashid
  • 13,731
  • 6
  • 37
  • 41
  • 1
    Thanks, but it doesn't work if you configured the diff to a external tool.. any ideas? – adardesign Jun 02 '13 at 19:32
  • 1
    @adardesign, I think that would probably have to be configured in the external tool. I'm not sure if there's anything git can do to present the diff without whitespace changes... could be wrong though; git is pretty powerful... – johnny Nov 01 '13 at 15:39
  • That would be nice to have it configured by default. I mean -w or -b or --ignore-all-space. There is a discussion about it at http://stackoverflow.com/questions/7310033/how-to-make-git-diff-ignore-space-change-the-default – Artyom Nov 26 '13 at 10:45
  • 10
    I agree with the -b suggestion, since -w treats "abc def" and "abcdef" as the same, which is rarely what I want! – IpsRich May 05 '15 at 10:32
15

This is an old question, but is still regularly viewed/needed. I want to post to caution readers like me that whitespace as mentioned in the OP's question is not the same as Regex's definition, to include newlines, tabs, and space characters -- Git asks you to be explicit. See some options here: https://git-scm.com/book/en/v2/Customizing-Git-Git-Configuration

As stated, git diff -b or git diff --ignore-space-change will ignore spaces at line ends. If you desire that setting to be your default behavior, the following line adds that intent to your .gitconfig file, so it will always ignore the space at line ends:

git config --global core.whitespace trailing-space

In my case, I found this question because I was interested in ignoring "carriage return whitespace differences", so I needed this:

git diff --ignore-cr-at-eol or git config --global core.whitespace cr-at-eol from here.

You can also make it the default only for that repo by omitting the --global parameter, and checking in the settings file for that repo. For the CR problem I faced, it goes away after check-in if warncrlf or autocrlf = true in the [core] section of the .gitconfig file.

deasserted
  • 661
  • 6
  • 6