在提交之间使用 git-diff 忽略 * all * 空格的更改

我正在浏览一个代码库,修复奇怪的空格,并且通常会修正缩进之类的东西,我想确保我没有无意中做出任何其他的更改,所以我正在使用 git diff -w来显示所有更改过的文件中的差异,同时忽略空格的差异。问题是,这实际上并没有忽略 所有的空格差异ーー至少 认为仅仅是空格差异。例如,在 git diff -w的以下输出中,

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

你可以看到我只有

  1. 删除多余的空白行,
  2. 将花括号放在打开其值的键的行尾,然后
  3. 缩进以适应上下文

这个问题 一开始看起来可能会提供一个答案,但是它处理的是两个特定 文件之间的差异,而不是两个特定 提交之间的差异。通过搜索发现的其他东西也都是死胡同。例如,这个问题处理的是合并,而不是显示差异,而 这个问题处理的是显示词级差异,等等。

61438 次浏览

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:]]'

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