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?
2 Answers
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".
-
1Thanks, but it doesn't work if you configured the diff to a external tool.. any ideas? 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...– johnnyCommented 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/…– ArtyomCommented Nov 26, 2013 at 10:45
-
10I agree with the -b suggestion, since -w treats "abc def" and "abcdef" as the same, which is rarely what I want!– IpsRichCommented May 5, 2015 at 10:32
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.
git diff -b
instead?git diff -b
is another name forgit diff --ignore-space-change