508

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?

7
  • 57
    Considered using git diff -b instead? Commented Nov 8, 2013 at 9:18
  • 14
    "-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
    Commented May 31, 2018 at 0:49
  • 10
    For those who don't know, git diff -b is another name for git diff --ignore-space-change
    – aafulei
    Commented Dec 4, 2020 at 2:28
  • 1
    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
    Commented Aug 2, 2021 at 9: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
    Commented Aug 4, 2021 at 17:40

2 Answers 2

583

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

4
  • 1
    Thanks, but it doesn't work if you configured the diff to a external tool.. any ideas?
    – adardesign
    Commented Jun 2, 2013 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
    Commented Nov 1, 2013 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 stackoverflow.com/questions/7310033/…
    – Artyom
    Commented Nov 26, 2013 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
    Commented May 5, 2015 at 10:32
18

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.

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