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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33159394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 03:38:45  来源:igfitidea点击:

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

gitdiffgit-diff

提问by iconoclast

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 -wto display differences in all changed files while ignoring whitespace differences. The problem is that this is not actually ignoring allwhitespace differences—at least what Iconsider to be merely whitespace differences. For instance, in the following output from git diff -w,

我正在检查代码库并修复空白的奇怪之处并通常更正缩进等,我想确保我没有无意中进行任何其他更改,所以我正在做的git diff -w是在忽略空白的同时显示所有更改文件中的差异差异。问题是这实际上并没有忽略所有空白差异——至少认为仅仅是空白差异。例如,在以下输出中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
  1. 删除多余的空行,
  2. 将花括号放在它们打开的键的行尾,并且
  3. 缩进以适应上下文

This questionlooked 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 questionis about merging, not displaying differences, and this questiondeals with displaying word-level differences, and so forth.

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

采纳答案by iconoclast

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. Create or edit the .gitconfigin your project, to include

首先,您必须控制 Git 当前使用的“空白”的定义。.gitconfig在您的项目中创建或编辑

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

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 exceptwhitespace differences) is not helpful. You can use -U0to tell Git to give you 0 lines of context, like so,

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

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.

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

回答by jww

Ignore allwhitespace 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忽略提交之间的所有空白更改

这个问题起初看起来可能会提供答案,但它处理两个特定文件之间的差异,而不是两个特定提交之间的差异。通过搜索发现的其他一切也都是死胡同......

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.

我认为您遇到了麻烦,因为 Git 是该工作的错误工具。它不会使任务变得容易,并且屈服以容纳该工具是错误的方法。工具应该为您工作,反之则不然

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

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

Here are some of the options for diff

以下是一些选项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