Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignore *all* whitespace changes with git-diff between commits

Tags:

git

git-diff

diff

I'm going through a codebase and fixing whitespace oddities and generally correcting indentation and such things, and I want to make sure I haven't inadvertently made any other changes, so I'm doing git diff -w to display differences in all changed files while ignoring whitespace differences. The problem is that this is not actually ignoring all whitespace differences—at least what I consider to be merely whitespace differences. For instance, in the following output from git diff -w,

-"Links": -{ - -    "Thermal": - -{ - +  "Links": { +    "Thermal": { 

you can see that I've only

  1. removed superfluous blank lines,
  2. put curly braces on the end of the line of the key whose value they open, and
  3. indented to fit the context

This question looked like it might offer an answer at first, but it deals with differences between two specific files, not between two specific commits. Everything else turned up by searching was a dead end as well. For instance, this question is about merging, not displaying differences, and this question deals with displaying word-level differences, and so forth.

like image 897
iconoclast Avatar asked Oct 15 '15 22:10

iconoclast


People also ask

How do I ignore whitespace in diff?

The --ignore-trailing-space ( -Z ) option ignores white space at line end. Re: "-w or --ignore-all-space option does not ignore newline-related changes" So -w ignores all whitespace, except for the whitespace it doesn't ignore.

How do I get rid of whitespace changes in git?

Right click on base branch in the Git tab, and do reset. It will undo the commit and stage non-whitespaces changes for commit. Now commit and push -f (I always force push via command line). That's it, you are done!

How do I ignore whitespace changes in Github?

On github, you simply append the w=1 parameter to the URL for it to ignore whitespace.

What is ignore whitespace?

-w, --ignore-all-space Ignore whitespace when comparing lines. This ignores differences even if one line has whitespace where the other line has none.


2 Answers

Perhaps there is a better answer, but the best solution I've found so far is this.

First, you must control the definition of "whitespace" that Git is currently using.

git config core.whitespace '-trailing-space,-indent-with-non-tab,-tab-in-indent' 

Next, you must control the definition of a word used. Instead of just using git diff -w, add --word-diff-regex='[^[:space:]]':

git diff -w --word-diff-regex='[^[:space:]]' 

You'll still see the context, which (in my case, since I'm trying to ensure that there are no differences except whitespace differences) is not helpful. You can use -U0 to tell Git to give you 0 lines of context, like so,

git diff -w -U0 --word-diff-regex='[^[:space:]]' 

but you'll still get output that looks pretty much like context, but it's still much better than looking through all the changes carefully and manually to make sure they are only whitespace changes.

You can also do all the above in one command. The -c flag changes git config just for one command.

git -c core.whitespace=-trailing-space,-indent-with-non-tab,-tab-in-indent diff -U0 --word-diff-regex='[^[:space:]]' 
like image 121
iconoclast Avatar answered Sep 20 '22 16:09

iconoclast


Ignore all whitespace changes with git-diff between commits

This question looked like it might offer an answer at first, but it deals with differences between two specific files, not between two specific commits. Everything else turned up by searching was a dead end as well....

I think you are having trouble because Git is the wrong tool for the job. It does not make the task easy, and yielding to accommodate the tool is the wrong approach. Tools are supposed to work for you and not vice versa.

Perform a second clone, and then checkout the starting revision in question. Then run regular diff on them using your current revision: diff -bur --ignore-all-space <dir1> <dir2>.

Here are some of the options for diff

-i, --ignore-case        ignore case differences in file contents  -E, --ignore-tab-expansion        ignore changes due to tab expansion  -Z, --ignore-trailing-space         ignore white space at line end  -b, --ignore-space-change        ignore changes in the amount of white space  -w, --ignore-all-space        ignore all white space  -B, --ignore-blank-lines        ignore changes where lines are all blank 
like image 33
jww Avatar answered Sep 16 '22 16:09

jww