忽略 *所有* 空格更改 git-提交之间的差异

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

我正在检查代码库并修复空白问题并通常更正缩进等问题,我想确保我没有无意中进行任何其他更改,所以我正在做 git diff -w显示所有更改文件的差异,同时忽略空白差异。问题是这实际上并没有忽略 all 空白差异——至少 I 认为仅仅是空白差异。例如,在 git diff -w

的以下输出中
-"Links":
-{
-
-    "Thermal":
-
-{
-
+  "Links": {
+    "Thermal": {

你可以看到我只有

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

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处理显示词级差异等。

也许有更好的答案,但目前我找到的最好的解决方案是这个。

首先,您必须控制 Git 当前正在使用的 "whitespace" 的定义。

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

接下来,你必须对照定义使用的一个词。不要只使用 git diff -w,而是添加 --word-diff-regex='[^[:space:]]':

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

你仍然会看到上下文,这(在我的例子中,因为我试图确保没有差异 除了 空白差异)没有帮助。您可以使用 -U0 告诉 Git 给您 0 行上下文,像这样,

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

但您仍然会得到看起来非常像上下文的输出,但它仍然比仔细和手动查看所有更改以确保它们只是空白更改要好得多。

您也可以在一个命令中完成上述所有操作。 -c 标志仅针对一个命令更改 git 配置。

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

我认为您遇到了麻烦,因为 Git 是这项工作的错误工具。它不会使任务变得容易,让步以适应该工具是错误的方法。工具应该为你工作而不是反之亦然

执行第二次克隆,然后签出有问题的起始修订版。然后 运行 使用您当前的版本对它们进行常规差异:diff -bur --ignore-all-space <dir1> <dir2>.

这里是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