Git diff -w 仅在行首和行尾忽略空格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4350678/
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
Git diff -w ignore whitespace only at start & end of lines
提问by ma11hew28
I love to use git diff -w
to ignore whitespace differences. But, I just noticed that it ignores even whitespace differences in the middle of lines. How could I only ignore whitespace differences that come at the start (^) or end ($) of lines?
我喜欢用来git diff -w
忽略空格差异。但是,我只是注意到它甚至忽略了行中间的空白差异。我怎么能只忽略行开头(^)或结尾($)的空白差异?
回答by Fake Code Monkey Rashid
For end of line use:
对于行尾使用:
git diff --ignore-space-at-eol
Instead of what are you using currently:
而不是你目前使用的是什么:
git diff -w (--ignore-all-space)
For start of line... you are out of luck if you want a built in solution.
首先……如果你想要一个内置的解决方案,你就不走运了。
However, if you don't mind getting your hands dirty there's a rather old patch floating out there somewhere that adds support for "--ignore-space-at-sol".
然而,如果你不介意弄脏你的手,有一个相当老的补丁漂浮在某个地方,增加了对“--ignore-space-at-sol”的支持。
回答by deasserted
This is an old question, but is still regularly viewed/needed. I want to post to caution readers like me that whitespaceas mentioned in the OP's question is notthe same as Regex's definition, to include newlines, tabs, and space characters -- Git asks you to be explicit. See some options here: https://git-scm.com/book/en/v2/Customizing-Git-Git-Configuration
这是一个老问题,但仍然经常被查看/需要。我想发布到提醒读者像我这样的空格作为OP的问题中提到的不一样的正则表达式的定义,包括新行,制表符和空格字符- Git会询问你要明确。在此处查看一些选项:https: //git-scm.com/book/en/v2/Customizing-Git-Git-Configuration
As stated, git diff -b
or git diff --ignore-space-change
will ignore spaces at line ends. If you desire that setting to be your default behavior, the following line adds that intent to your .gitconfig file, so it will always ignore the space at line ends:
如上所述,git diff -b
或git diff --ignore-space-change
将忽略行尾的空格。如果您希望该设置成为您的默认行为,以下行将该意图添加到您的 .gitconfig 文件中,因此它将始终忽略行尾的空格:
git config --global core.whitespace trailing-space
git config --global core.whitespace trailing-space
In my case, I found this question because I was interested in ignoring "carriage return whitespace differences", so I needed this:
就我而言,我发现这个问题是因为我对忽略“回车空白差异”感兴趣,所以我需要这个:
git diff --ignore-cr-at-eol
or
git config --global core.whitespace cr-at-eol
from here.
git diff --ignore-cr-at-eol
或
git config --global core.whitespace cr-at-eol
从这里。
You can also make it the default only for that repoby omitting the --global parameter, and checking in the settings file for that repo. For the CR problem I faced, it goes away after check-in if warncrlf or autocrlf = true in the [core] section of the .gitconfig file.
您还可以通过省略 --global 参数并签入该存储库的设置文件,使其仅成为该存储库的默认值。对于我遇到的 CR 问题,如果 .gitconfig 文件的 [core] 部分中的 warncrlf 或 autocrlf = true ,则它会在签入后消失。