Git 认为每次我做一个小改动时我都在重写我的一个文件

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

Git thinks I am rewriting one of my files everytime I make a small change

gitdiff

提问by Paul Wicks

I have a medium size Java file. Everytime I make a change to one of my files, BuildTable.java, Git reports it as a massive change, even if is only a line or two. BuildTable.java is about 200 lines and the change in this commit only changed a single line.

我有一个中等大小的 Java 文件。每次我对我的一个文件 BuildTable.java 进行更改时,Git 都会将其报告为一个巨大的更改,即使只有一两行。BuildTable.java 大约有 200 行,这次提交中的更改仅更改了一行。

git-diff ouputs this:

git-diff 输出这个:

--- a/src/BuildTable.java
+++ b/src/BuildTable.java
@@ -1 +1 @@
-import java.io.FileNotFoundException;^Mimport java.io.FileReader;^Mimport java.io.InputStreamReader;^Mimport java.io.PushbackReader;^Mimport java.util.ArrayList;^Mimport
\ No newline at end of file
+import java.io.FileNotFoundException;^Mimport java.io.FileReader;^Mimport java.io.InputStreamReader;^Mimport java.io.PushbackReader;^Mimport java.util.ArrayList;^Mimport
\ No newline at end of file

After doing a git-commit -a

执行 git-commit -a 后

Created commit fe43985: better error notifications
 3 files changed, 54 insertions(+), 50 deletions(-)
 rewrite src/BuildTable.java (78%)

Is Git seeing this file as binary or something? Is this a problem? If it is, how do I fix this?

Git 是否将此文件视为二进制文件或其他文件?这是一个问题吗?如果是,我该如何解决这个问题?

回答by ddaa

Clearly, git does not like your mac-style line endings (CR only). Its diff algorithm uses LF as the line separator.

显然,git 不喜欢你的 mac 风格的行尾(仅限 CR)。它的 diff 算法使用 LF 作为行分隔符。

Fix your files to have windows-style (CR LF) or unix (LF only) line endings.

修复您的文件以具有 Windows 样式(CR LF)或 unix(仅 LF)行尾。

回答by Paul Wicks

To fix this, I didn't need to change any of the core git settings, as the default line endings being generated were fine, it was just that this particular file was mangled. To fix it I opened vim and executed the following command

为了解决这个问题,我不需要更改任何核心 git 设置,因为生成的默认行结尾很好,只是这个特定文件被破坏了。为了修复它,我打开了 vim 并执行了以下命令

:%s/^M/\r/g

Note that to type the "^M" you have to type ctrl-V and then ctrl-M.

请注意,要键入“^M”,您必须先按 ctrl-V,然后按 ctrl-M。

回答by David Schmitt

Set core.autocrlfand core.safecrlfwith git-config. This will cause git to automatically convert line endings when transferring from/to the object store. You might need to make a commit to store the "new" endings.

设置core.autocrlfcore.safecrlf使用git-config。这将导致 git 在从/向对象存储传输时自动转换行尾。您可能需要提交以存储“新”结尾。

Judging from your pasted example, you might be also suffering from "old-style Mac line endings" (thanks to ddaaand Charles Baileyfor the hint), which are only bare CRs without any LF, a case not handled by git. If this is true (check with a hex editor), use a tool like recodeto translate this garbage into some 21st century format, like proper LF-only Unix line endings.

从您粘贴的示例来看,您可能还会遇到“旧式 Mac 行尾”(感谢ddaaCharles Bailey的提示),它们只是CR没有任何 s 的裸s ,这是LFgit 未处理的情况。如果这是真的(用十六进制编辑器检查),使用类似的工具recode将这些垃圾转换成某种 21 世纪的格式,比如正确的LFUnix 行尾。

回答by Andrew Grimm

git diff -b

Ignores end of line changes when showing you the differences.

显示差异时忽略行尾更改。