git diff - 显示行结束更改?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3920650/
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-10 09:16:12  来源:igfitidea点击:

git diff - show me line ending changes?

gitline-endingsgit-diff

提问by Stonky

My editor is changing the line endings of my source files. When I do git diff, I see the same line twice -- once with -and once with +-- with no visible difference.

我的编辑器正在更改我的源文件的行尾。当我这样做时git diff,我看到同一行两次——一次-和一次+——没有明显的区别。

How do I get git diffto show me what this change actually was?

我如何才能git diff向我展示这种变化实际上是什么?

回答by Paul Whittaker

First, make sure you're using the coloured output (e.g. with git diff --color) and that you've enabled whitespace highlighting with (e.g.)

首先,确保您使用的是彩色输出(例如 with git diff --color)并且您已经启用了空白突出显示(例如)

git config color.diff.whitespace "red reverse"

This might not work in all cases, however, as gitdoesn't appear to highlight trailing whitespace for removedlines. To see whitespace that you've deleted, simply use

然而,这可能并不适用于所有情况,因为git似乎不会突出显示已删除行的尾随空格。要查看已删除的空格,只需使用

git diff -R

to put the whitespace on the 'added' side of the comparison, where it does get highlighted.

将空格放在比较的“添加”侧,它确实会突出显示。

For more detail, see the answers at this SO question.

有关更多详细信息,请参阅此 SO 问题的答案。

回答by droid7c2

You can see line-ending difference with the following command.

您可以使用以下命令查看行尾差异。

git diff | cat -v

Then "^M" is printed for CRLF (DOS) ending, nothing for LF (Unix) ending.

然后为 CRLF (DOS) 结尾打印 "^M",对于 LF (Unix) 结尾则不打印。

Apparently git diff is doing the right thing, printing CR and LF characters for CRLF ending. But because CR is consumed by the console, we cannot see it. By using cat -v, we can make it visible.

显然 git diff 正在做正确的事情,为 CRLF 结尾打印 CR 和 LF 字符。但是因为 CR 是被控制台消费的,所以我们看不到它。通过使用 cat -v,我们可以使其可见。

回答by ntc2

One way to see whitespace changes is to do a character-by-character "word diff" with

查看空格变化的一种方法是逐个字符地进行“单词差异”

git diff --color --word-diff-regex=.

This highlights all whitespace changes everywhere in lines. Removed whitespace is wrapped in [-and -]and added whitespace in {+and +}.

这将突出显示行中各处的所有空白更改。去除空格被包裹在[--]和在添加空格{++}

Alternatively, as suggested by Alex

或者,按照亚历克斯的建议

git diff --color --ws-error-highlight=new,old

highlights all whitespace changes at the ends of lines.

亮点所有空白改变线的终端

回答by Alex

git diff --ws-error-highlight=new,old

highlights whitespace diffs in changed lines.

突出显示更改行中的空白差异。

回答by bstpierre

A graphical diff tool will show you the change better -- try git difftool.

图形差异工具将更好地向您显示更改 - 尝试git difftool

Use meld, and set the preferences to show whitespace. (Edit -> Preferences -> Show Whitespace.)

使用meld,并设置首选项以显示空格。(编辑 -> 首选项 -> 显示空白。)

Other graphical tools probably have similar options -- @Cotton's answer+comment tells you how to do this with vimdiff.

其他图形工具可能有类似的选项——@Cotton 的回答+评论告诉你如何使用 vimdiff 做到这一点。