git diff 显示 ^M,颜色为 ui true

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

git diff shows ^M with color ui true

gitvim

提问by ezdazuzena

I modified my .gitconfigin such a way that I have some colors when performing git diff:

我修改了我.gitconfig的方式,在执行时我有一些颜色git diff

$ cat .gitconfig 
[color]
ui = true

I'm working on an ubuntu machine and I edited some code using VIM. After editing a file a execute git diff, once with and once without ui=true.

我在 ubuntu 机器上工作,我使用 VIM 编辑了一些代码。编辑文件后执行git diff一次,一次有一次没有ui=true

Problem: in the first case I have ^Mcharacters and the end of the edited lines. However, I don't see those when turning of color.uior when looking with vim, cat, more.. at the manipulated file.

问题:在第一种情况下,我有^M字符和编辑行的结尾。但是,在打开color.ui或使用vim, cat, more.. 查看操作文件时,我看不到这些内容。

回答by Xavier T.

It is probably an encoding issue. The 'git diff' command is executing Vim thinking that the file format is Dos.

应该是编码问题。'git diff' 命令正在执行 Vim,认为文件格式是 Dos。

When you are using any other command, it is correctly recognized as a Unix file.

当您使用任何其他命令时,它会被正确识别为 Unix 文件。

Can you try : :set fileformat=unixin your git diff window ?

您可以尝试: :set fileformat=unix在您的 git diff 窗口中吗?

I am not really sure this is the root cause, because I don't see the link with the uioption.

我不确定这是根本原因,因为我没有看到该ui选项的链接。

回答by sparrowt

The problem was solved for me by setting core.whitespaceto cr-at-eol

这个问题是通过设置解决了我core.whitespacecr-at-eol

treats a carriage-return at the end of line as part of the line terminator (git-config docs)

将行尾的回车符视为行终止符的一部分(git-config 文档)

This can be added to a per-project or global git config file as follows:

这可以添加到每个项目或全局 git 配置文件中,如下所示:

[core]
    whitespace = cr-at-eol

This solves the problem of hiding the ^M at the end of lines which were in the diff due to another non-line-ending change. The intention is not to ignore changes where the only difference is the line ending. I am on windows with core.autocrlf=trueand so the line ending mismatch is expected.

这解决了由于另一个非行结束更改而在差异中的行末尾隐藏 ^M 的问题。目的不是忽略唯一区别是行尾的更改。我在带有core.autocrlf= 的Windows 上true,因此预计行结束不匹配。

My only caution is that I don't know if this will affect whether git flags up genuine changes in EOL which one might want to commit, but perhaps with autocrlf=true this is never going to be the case.

我唯一要注意的是,我不知道这是否会影响 git 是否会在 EOL 中标记人们可能想要提交的真正更改,但也许 autocrlf=true 永远不会出现这种情况。

An alternative fix, more targeted (but slightly more hacky), is described here.

此处描述另一种修复,更有针对性(但稍微有点老套)。

回答by user1338062

Please have a look at Github's excellent "Dealing with line endings" page:

请查看 Github 出色的“处理行尾”页面:

https://help.github.com/articles/dealing-with-line-endings

https://help.github.com/articles/dealing-with-line-endings

You probably want to set core.autocrlf and then re-normalize your repository.

您可能想要设置 core.autocrlf,然后重新规范化您的存储库。