忽略所有*空格在提交之间使用git-diff更改 [英] Ignore *all* whitespace changes with git-diff between commits

查看:613
本文介绍了忽略所有*空格在提交之间使用git-diff更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在经历一个代码库,修复空白的怪异,并且通常会纠正缩进和其他事情,并且我想确保我没有无意中做出任何其他更改,所以我正在执行 git diff -w 显示所有已更改文件的差异,同时忽略空格的差异。问题是这实际上并没有忽略所有的空白区别 - 至少我认为这只是空白区别。例如,在 git diff -w

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": {

你可以看到我只有 p>

you can see that I've only


  1. 删除了多余的空白行,
  2. 将大括号放在密钥行的末尾它们打开并缩小以符合上下文

这个问题看起来好像它可能会首先提供一个答案,但它处理两个特定的文件之间的差异,而不是两个特定的提交。搜索出现的其他一切也都是死路一条。例如,这个问题是关于合并,不显示差异的,这个问题涉及显示字级差异等等。

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.

推荐答案

也许有一个更好的答案,但我迄今发现的最好的解决方案是这样的。

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

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

First, you must control the definition of "whitespace" that Git is currently using. Create or edit the .gitconfig in your project, to include

[core]
    whitespace = -trailing-space,-indent-with-non-tab,-tab-in-indent

接下来,您必须控制所用单词的定义。而不是仅仅使用 git diff -w ,添加 - word-diff-regex = [^ [:space:]]

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

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

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.

这篇关于忽略所有*空格在提交之间使用git-diff更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆