简体   繁体   English

git:编辑文件导致^ M在使用git diff时出现

[英]git: editing a file cause ^M to appear when using git diff

I have a couple of source files inside a local git repo. 我在本地git仓库中有几个源文件。 The files have been pulled from a remote git repo. 这些文件是从远程git仓库中提取的。 When I do a "$git status" I see the following: 当我执行“$ git status”时,我会看到以下内容:

$git status

# On branch master
nothing to commit, working directory clean

However, when I edit any source file (using vim or gedit), ^M always gets added to the end of lines that I add. 但是,当我编辑任何源文件(使用vim或gedit)时,^ M总是被添加到我添加的行的末尾。 In other words, after I make the changes I see the following: 换句话说,在我进行更改后,我看到以下内容:

$git status

# On branch master
# Changes not staged for commit: 
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   sourcefile1.c
#
no changes added to commit (use "git add" and/or "git commit -a")

$git diff sourcefile1.c
...
+ This is a test line ^M
...

I do not know why this is happening but it seems that it is not related to the editor I use nor git itself. 我不知道为什么会这样,但它似乎与我使用的编辑器无关,也不与git本身有关。 I am aware that there is a way to force git to ignore ^M , but I really want to know what is going wrong, why these ^M are being inserted, and how I can stop that from happening. 我知道有一种方法可以强制git忽略^ M ,但我真的想知道出了什么问题,为什么要插入这些^ M,以及如何阻止它发生。

I am using Ubuntu with Linux kernel 3.11.0 我在Linux内核3.11.0上使用Ubuntu

I appreciate any help. 我感谢任何帮助。

When you do a git-diff it will warn if there is a CR (the ^M) at the end of a line. 当你执行git-diff时,如果在行尾有一个CR(^ M),它会发出警告。 This is considered a "whitespace error" and will show up in RED by default irrespective of whether there is a real diff. 这被认为是“空白错误”并且默认情况下将以红色显示,而不管是否存在真正的差异。 You can get it to shut up by creating a .gitattributes file containing the line 您可以通过创建包含该行的.gitattributes文件来使其关闭

* whitespace=cr-at-eol

This won't affect the file contents, it just disables the warning from git-diff. 这不会影响文件内容,只是禁用git-diff的警告。

You don't say whether the file originally had CRLF line endings, but it is highly unlikely that Vim converted it from Unix to DOS line endings without warning you. 你没有说文件最初是否有CRLF行结尾,但是Vim很可能不会在没有警告的情况下将它从Unix转换为DOS行结尾。 I find line ending conversions in git to be a real pain and confusing and just switch them off in .gitattributes by adding a line 我在git中发现行结束转换是一个真正的痛苦和混乱,只需通过添加一行就可以在.gitattributes关闭它们

* -text

This stops git doing any magic conversions and leaves it up to you. 这会阻止git进行任何魔法转换,并将其留给您。

You probably want to check all the files in your working directory to see what line endings they currently have (Vim should tell you in the status bar). 您可能希望检查工作目录中的所有文件,以查看它们当前具有哪些行结尾(Vim应该在状态栏中告诉您)。 See also Git line endings after normalization for a couple of git aliases that can convert from one to the other. 在标准化之后还可以看到几个可以从一个转换为另一个的git别名的Git行结尾 If this is a Linux-only repo doing a one-off conversion of everything to Unix format might be a good idea. 如果这是一个仅限Linux的仓库,那么将所有内容一次性转换为Unix格式可能是一个好主意。

I have faced this problem once and I was able to solve it with the "dos2unix" program. 我曾经遇到过这个问题,我能用“dos2unix”程序解决它。 I think FDinoff is right in that vim uses the dos line endings because the file originally has the dos format. 我认为FDinoff是正确的,因为vim使用dos行结尾,因为该文件最初具有dos格式。 To change it back to a unix format, install dos2unix (if you don't already have it). 要将其更改回unix格式,请安装dos2unix(如果您还没有)。 For Ubuntu, you can do this by: 对于Ubuntu,您可以通过以下方式执行此操作:

sudo apt-get install dos2unix

Then use this command to convert your file format, eg 然后使用此命令转换文件格式,例如

dos2unix sourcefile1.c

After this, if the file format was actually the problem, you should not see the ^M when you add new lines anymore. 在此之后,如果文件格式实际上是问题,则在添加新行时不应该看到^ M.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM