为 git-diff 输出中的空白着色
关于代码格式,我是纯粹主义者:)。我经常删除不必要的空格(仅包含 ws 的行、行尾的 ws 等)。我什至将 vim 设置为显示那种红色的线条。
我的问题是,使用 git-diff 我经常看到这样的东西:
- else{
+ else{
即使我有 git-diff 颜色,我也看不到差异(在这种特殊情况下,我在行尾删除了 1 ws)。有什么方法可以告诉 git-diff 显示 ws 颜色为红色吗? (例如与 /\s+$/ 正则表达式匹配的那些)。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
使用Git 2.11(2016 年第 4 季度) 及更高版本,您可以执行以下操作:
请参阅关于
git diff
和 关于git配置
。对于早于该版本的版本,您可以设置
color.diff.whitespace
配置设置,例如:(我假设您已经有
color.diff
或color.ui
设置为auto
因为你说你无论如何都能看到来自git diff
的彩色补丁。)如果你想微调空白错误的类型以红色突出显示的,然后您可以更改
core.whitespace
,但默认情况下启用blank-at-eol
,因此您可能不需要更改它你提到的例子。一个可能造成混淆的原因是,在 git diff 的输出中,空白错误仅在引入的行中突出显示,而在删除的行中则不突出显示。 (更新:正如 Paul Whittaker 在他的回答中指出的那样,您应该投票: ),您可以通过使用 git diff -R 反转 diff 的含义来查看这些内容。)
您可以在 git config 手册页
如果您不想使用
-R
组合,您可以使用 WhiteSpace diff 手册页中的错误突出显示选项。git diff --ws-error-highlight=new,old
或
git diff --ws-error-highlight=all
对于旧版本从 2.11 开始,除了使用别名之外,没有办法永久打开此功能并将其存储在配置中:
git config alias.df 'diff --ws-error-highlight=all'
现在您可以使用:
git df
查看红色的变化。
With with Git 2.11 (Q4 2016) and after, you can do:
See doc on
git diff
and ongit config
.For versions older than that, you can set the
color.diff.whitespace
config setting, e.g. with:(I'm assuming that you already have
color.diff
orcolor.ui
set toauto
since you say that you see coloured patches fromgit diff
anyway.)If you want to fine tune the type of whitespace errors that are highlighted in red, you can then change
core.whitespace
, butblank-at-eol
is enabled by default so you probably won't need to change that for the example you mention.A possible source of confusion is that in the output of
git diff
, whitespace errors are only highlighted in the lines that are introduced, not those that are removed. (Update: as Paul Whittaker points out in his answer, which you should up-vote :), you can see these by reversing the sense of the diff withgit diff -R
.)You can find more documentation on these config options in the git config man page
If you don't want to use the
-R
kludge you can use the WhiteSpace Error Highlight option from the diff man page.git diff --ws-error-highlight=new,old <file>
or
git diff --ws-error-highlight=all <file>
With versions older than 2.11, there’s no way to permanently turn this on and store this in config aside from using an alias:
git config alias.df 'diff --ws-error-highlight=all'
Now you can use:
git df <file>
To see the changes in red.
使用 git diff -R 将删除的行变成添加的行。然后尾随空白将突出显示。
(这假设您已经启用了空白突出显示,根据 Mark 答案中的颜色设置。此方法的功劳来自 Junio 的帖子 http://git.661346.n2.nabble.com/Highlighting-whitespace-on-removal-with-git-diff-td5653205.html .)
例如,当将文件从 DOS 行结尾转换为 Unix 时,
git diff -R
清楚地显示了出现在末尾的^M
字符(dis)线。如果没有-R
(也没有-w
等),它会显示整个文件已更改,但不会显示如何更改。Use
git diff -R
to turn removed lines into added lines. Then trailing whitespace will be highlighted.(This assumes you already have whitespace hightlighting enabled, as per the colour settings from Mark's answer. Credit for this method goes to Junio's post at http://git.661346.n2.nabble.com/Highlighting-whitespace-on-removal-with-git-diff-td5653205.html .)
For example, when converting a file from DOS line endings to Unix,
git diff -R
clearly shows me the^M
characters (dis)appearing at the ends of lines. Without-R
(and also without-w
etc.) it shows that the entire file has changed, but doesn't show how.对于懒惰的答案浏览器,只需运行:
然后 git diff 也会突出显示已删除行中的尾随空格。
For the lazy answer skimmers, just run:
Then
git diff
will highlight trailing whitespaces in removed lines as well.使用 git diff --color |少-R。
-R
使颜色控制代码变得人性化。然后就可以使用
less
的正则表达式搜索,egUse
git diff --color | less -R
. The-R
makes the color control codes human-friendly.Then you can use
less
's regular expression search, e.g.我的 git diff 版本似乎已经做到了这一点 - 我有 git 1.7.4.1 并设置了 color.ui = auto 。
My version of
git diff
already seems to do this - I have git 1.7.4.1 and have setcolor.ui = auto
.上面提到的命令也可以包含在gitconfig中
而不是将
相应的选项添加到例如 ~/.gitconfig 中
文件作为选项,即上面的两个选项应该可以解决问题, 。我测试的最早的 git 版本是 1.7,但也应该适用于该版本之后的任何版本。在最新版本的 git 上,“[color]”选项默认设置为“auto”。
The command mentioned above can also be included in the gitconfig
file as options, i.e instead of
adding the corresponding options to e.g ~/.gitconfig
the two options above should do the trick. The earliest git version I tested this was 1.7 but should work for any version after that too. On recent versions of git the "[color]" options is set to "auto" by default.