我有一个可以从 Windows 和 OS X 访问的 Git 存储库,并且我知道它已经包含一些带有 CRLF 行结尾的文件。据我所知,有两种方法可以解决这个问题:
-
将core.autocrlf
设置为false
,
-
按照说明操作此处(在 GitHub 的帮助页面上回显)将存储库转换为仅包含 LF 行结尾,然后设置 core.autocrlf
在 Windows 上为 true
,在 OS X 上为 input
。这样做的问题是,如果我在存储库中有任何二进制文件:
- 在 gitattributes 中未正确标记为二进制,并且
- 恰好同时包含 CRLF 和 LF,
它们将会被损坏。我的存储库可能包含此类文件。
那么为什么我不应该关闭 Git 的行结束转换呢?网络上有很多关于关闭 core.autocrlf 会导致问题的模糊警告,但很少有具体的警告;到目前为止我发现的唯一问题是 kdiff3 无法处理 CRLF 结尾(对我来说不是问题),并且某些文本编辑器存在行结束问题(对我来说也不是问题)。
该存储库是我公司内部的,因此我无需担心与具有不同 autocrlf 设置或行结束要求的人员共享它。
只保留行结尾是否还有我不知道的其他问题?
I have a Git repository that is accessed from both Windows and OS X, and that I know already contains some files with CRLF line-endings. As far as I can tell, there are two ways to deal with this:
-
Set core.autocrlf
to false
everywhere,
-
Follow the instructions here (echoed on GitHub's help pages) to convert the repository to contain only LF line-endings, and thereafter set core.autocrlf
to true
on Windows and input
on OS X. The problem with doing this is that if I have any binary files in the repository that:
- are not correctly marked as binary in gitattributes, and
- happen to contain both CRLFs and LFs,
they will be corrupted. It is possible my repository contains such files.
So why shouldn't I just turn off Git's line-ending conversion? There are a lot of vague warnings on the web about having core.autocrlf
switched off causing problems, but very few specific ones; the only that I've found so far are that kdiff3 cannot handle CRLF endings (not a problem for me), and that some text editors have line-ending issues (also not a problem for me).
The repository is internal to my company, and so I don't need to worry about sharing it with people with different autocrlf settings or line-ending requirements.
Are there any other problems with just leaving line-endings as-is that I am unaware of?
发布评论
评论(5)
将
autocrlf
设置为true
的唯一具体原因是:git status
将所有文件显示为已修改
,因为将基于 Unix 的 EOL Git 存储库克隆到 Windows 存储库时完成的自动 EOL 转换(请参阅 issue 83)除非您能看到必须处理本机 EOL 的特定处理方法,否则您最好将
autocrlf
保留为false
(git config --global core.autocrlf false
)。请注意,此配置将是本地配置(因为配置不会从存储库推送到存储库)
如果您希望克隆该存储库的所有用户使用相同的配置,请查看“git 的最佳
CRLF
处理策略是什么?",使用 中的text
属性.gitattributes
文件。示例:
注意:从 git 2.8(2016 年 3 月)开始,合并标记将不再在 CRLF 文件中引入混合行结尾 (LF)。
请参阅“让 Git 在其“<<<<<”
The only specific reasons to set
autocrlf
totrue
are:git status
showing all your files asmodified
because of the automatic EOL conversion done when cloning a Unix-based EOL Git repo to a Windows one (see issue 83 for instance)Unless you can see specific treatment which must deal with native EOL, you are better off leaving
autocrlf
tofalse
(git config --global core.autocrlf false
).Note that this config would be a local one (because config isn't pushed from repo to repo)
If you want the same config for all users cloning that repo, check out "What's the best
CRLF
handling strategy with git?", using thetext
attribute in the.gitattributes
file.Example:
Note: starting git 2.8 (March 2016), merge markers will no longer introduce mixed line ending (LF) in a CRLF file.
See "Make Git use CRLF on its “<<<<<<< HEAD” merge lines"
我是一名 .NET 开发人员,多年来一直使用 Git 和 Visual Studio。我强烈建议将行结尾设置为 true。并在存储库的生命周期内尽早执行此操作。
话虽这么说,我讨厌 Git 改变我的行结尾。源代码管理应该只保存和检索我所做的工作,而不应该修改它。曾经。但确实如此。
如果您没有将每个开发人员设置为 true,会发生什么情况,最终会有一个开发人员设置为 true。这将开始将存储库中所有文件的行结尾更改为 LF。当用户将其设置为 false 时,Visual Studio 会警告您,并要求您更改它们。你很快就会发生两件事。第一,你会收到越来越多的警告,你的团队越大,收到的警告就越多。第二,也是更糟糕的事情是,它将显示每个修改文件的每一行都已更改(因为每一行的行结尾都会被真正的人更改)。最终,您将无法再可靠地跟踪存储库中的更改。让每个人都保持真实比试图让每个人都虚假要容易和干净得多。接受您所信任的源代码控制正在做一些不应该做的事情这一事实是可怕的。曾经。
I am a .NET developer and have used Git and Visual Studio for years. My strong recommendation is to set line endings to true. And do it as early as you can in the lifetime of your Repository.
That being said, I HATE that Git changes my line endings. Source control should only save and retrieve the work I do, it should NOT modify it. Ever. But it does.
What will happen if you don't have every developer set to true, is ONE developer eventually will set to true. This will begin to change the line endings of all of your files to LF in your repo. And when users set to false check those out, Visual Studio will warn you, and ask you to change them. You will have 2 things happen very quickly. One, you will get more and more of those warnings, the bigger your team the more you get. The second, and worse thing, is that it will show that every line of every modified file was changed(because the line endings of every line will be changed by the true guy). Eventually, you won't be able to track changes in your repo reliably anymore. It is MUCH easier and cleaner to make everyone keep to true than to try to keep everyone false. As horrible as it is to live with the fact that your trusted source control is doing something it should not. Ever.
更新 2:
Xcode 9 似乎有一个“功能”,它将忽略文件当前的行结尾,而是在将行插入文件时仅使用默认的行结尾设置,从而导致文件带有混合行结尾。
我很确定 Xcode 7 中不存在这个错误;不确定 Xcode 8。好消息是它似乎在 Xcode 10 中得到了修复。
在它存在的时候,这个错误在我在问题中引用的代码库中引起了少量的欢闹(迄今为止使用 < code>autocrlf=false),并导致许多“EOL”提交消息,并最终导致我编写了一个 git
pre-commit
挂钩来检查/防止引入混合行结尾。更新:
注意:正如 VonC 所指出的,从 Git 2.8 开始,合并标记将不会 将 Unix 风格的行尾引入 Windows 风格的文件。
原始:
我在这个设置中注意到的一个小问题是,当存在合并冲突时,git 添加来标记差异的行没有有 Windows 行 -结尾,即使文件的其余部分这样做,并且您最终可能会得到一个具有混合行结尾的文件,例如:
这不会给我们带来任何问题(我想任何可以处理这两种类型的行结尾的工具也将合理地处理混合行结尾——当然我们使用的所有行结尾都是如此),但这是需要注意的事情。
我们发现的另一件事*是,当使用
git diff
查看对具有Windows行结尾的文件的更改时,已添加的行会显示其回车符返回,因此:* 它并不真正值得使用术语:“问题”。
Update 2:
Xcode 9 appears to have a "feature" where it will ignore the file's current line endings, and instead just use your default line-ending setting when inserting lines into a file, resulting in files with mixed line endings.
I'm pretty sure this bug didn't exist in Xcode 7; not sure about Xcode 8. The good news is that it appears to be fixed in Xcode 10.
For the time it existed, this bug caused a small amount of hilarity in the codebase I refer to in the question (which to this day uses
autocrlf=false
), and led to many "EOL" commit messages and eventually to my writing a gitpre-commit
hook to check for/prevent introducing mixed line endings.Update:
Note: As noted by VonC, starting from Git 2.8, merge markers will not introduce Unix-style line-endings to a Windows-style file.
Original:
One little hiccup that I've noticed with this setup is that when there are merge conflicts, the lines git adds to mark up the differences do not have Windows line-endings, even when the rest of the file does, and you can end up with a file with mixed line endings, e.g.:
This doesn't cause us any problems (I imagine any tool that can handle both types of line-endings will also deal sensible with mixed line-endings--certainly all the ones we use do), but it's something to be aware of.
The other thing* we've found, is that when using
git diff
to view changes to a file that has Windows line-endings, lines that have been added display their carriage returns, thus:* It doesn't really merit the term: "issue".
在我们的项目中,我们声明有必要将 autocrlf 设置为输入值。您可能对此感兴趣的原因如下:
我们不久前遇到的问题:
In our project we declared necessity of setting autocrlf to input value. Here is why you may be interested in this:
The issues we faced not long ago:
为我。
编辑 .gitattributes 文件。
添加
然后一切顺利。
For me.
Edit .gitattributes file.
add
Then everything goes well.