为什么要在 Git 中使用 core.autocrlf=true ?

发布于 2024-09-01 09:08:21 字数 841 浏览 3 评论 0 原文

我有一个可以从 Windows 和 OS X 访问的 Git 存储库,并且我知道它已经包含一些带有 CRLF 行结尾的文件。据我所知,有两种方法可以解决这个问题:

  1. core.autocrlf设置为false

  2. 按照说明操作此处(在 GitHub 的帮助页面上回显)将存储库转换为仅包含 LF 行结尾,然后设置 core.autocrlf 在 Windows 上为 true,在 OS X 上为 input。这样做的问题是,如果我在存储库中有任何二进制文件:

    1. 在 gitattributes 中未正确标记为二进制,并且
    2. 恰好同时包含 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:

  1. Set core.autocrlf to false everywhere,

  2. 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:

    1. are not correctly marked as binary in gitattributes, and
    2. 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

扬花落满肩 2024-09-08 09:08:21

autocrlf 设置为 true 的唯一具体原因是:

  • 避免 git status 将所有文件显示为已修改,因为将基于 Unix 的 EOL Git 存储库克隆到 Windows 存储库时完成的自动 EOL 转换(请参阅 issue 83
  • 并且您的编码工具在某种程度上取决于本机 EOL文件中存在的样式:

除非您能看到必须处理本机 EOL 的特定处理方法,否则您最好autocrlf 保留为 false (git config --global core.autocrlf false)。

请注意,此配置将是本地配置(因为配置不会从存储库推送到存储库)

如果您希望克隆该存储库的所有用户使用相同的配置,请查看“git 的最佳 CRLF 处理策略是什么?",使用 中的 text 属性.gitattributes 文件

示例:

*.vcproj    text eol=crlf
*.sh        text eol=lf

注意:从 git 2.8(2016 年 3 月)开始,合并标记将不再在 CRLF 文件中引入混合行结尾 (LF)。
请参阅“让 Git 在其“<<<<<

The only specific reasons to set autocrlf to true are:

  • avoid git status showing all your files as modified because of the automatic EOL conversion done when cloning a Unix-based EOL Git repo to a Windows one (see issue 83 for instance)
  • and your coding tools somehow depends on a native EOL style being present in your file:

Unless you can see specific treatment which must deal with native EOL, you are better off leaving autocrlf to false (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 the text attribute in the .gitattributes file.

Example:

*.vcproj    text eol=crlf
*.sh        text eol=lf

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"

小…楫夜泊 2024-09-08 09:08:21

我是一名 .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.

維他命╮ 2024-09-08 09:08:21

更新 2

Xcode 9 似乎有一个“功能”,它将忽略文件当前的行结尾,而是在将行插入文件时仅使用默认的行结尾设置,从而导致文件带有混合行结尾。

我很确定 Xcode 7 中不存在这个错误;不确定 Xcode 8。好消息是它似乎在 Xcode 10 中得到了修复。

在它存在的时候,这个错误在我在问题中引用的代码库中引起了少量的欢闹(迄今为止使用 < code>autocrlf=false),并导致许多“EOL”提交消息,并最终导致我编写了一个 git pre-commit 挂钩来检查/防止引入混合行结尾。

更新

注意:正如 VonC 所指出的,从 Git 2.8 开始,合并标记不会 将 Unix 风格的行尾引入 Windows 风格的文件

原始

我在这个设置中注意到的一个小问题是,当存在合并冲突时,git 添加来标记差异的行没有有 Windows 行 -结尾,即使文件的其余部分这样做,并且您最终可能会得到一个具有混合行结尾的文件,例如:

// Some code<CR><LF>
<<<<<<< Updated upstream<LF>
// Change A<CR><LF>
=======<LF>
// Change B<CR><LF>
>>>>>>> Stashed changes<LF>
// More code<CR><LF>

这不会给我们带来任何问题(我想任何可以处理这两种类型的行结尾的工具也将合理地处理混合行结尾——当然我们使用的所有行结尾都是如此),但这是需要注意的事情。

我们发现的另一件事*是,当使用git diff查看对具有Windows行结尾的文件的更改时,已添加的行会显示其回车符返回,因此:

    // Not changed

+   // New line added in^M
+^M
    // Not changed
    // Not changed

* 它并不真正值得使用术语:“问题”。

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 git pre-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.:

// Some code<CR><LF>
<<<<<<< Updated upstream<LF>
// Change A<CR><LF>
=======<LF>
// Change B<CR><LF>
>>>>>>> Stashed changes<LF>
// More code<CR><LF>

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:

    // Not changed

+   // New line added in^M
+^M
    // Not changed
    // Not changed

* It doesn't really merit the term: "issue".

凉墨 2024-09-08 09:08:21

在我们的项目中,我们声明有必要将 autocrlf 设置为输入值。您可能对此感兴趣的原因如下:

  • 您的项目在 Linux/Unix 环境中运行,开发人员在 Windows 中编写代码。在推送过程中,CRLF 会自动转换为 LF,因此您只需在 Unix 服务器上查看 Git 存储库,一切都会开箱即用。
  • 有时,开发人员使用 WinSCP 手动将配置从 Windows 计算机上传到服务器。这会导致 Unix 机器上出现 CRLF 行结尾以及应用程序启动失败。 “input”值部分消除了此类情况的数量

我们不久前遇到的问题:

  • 如果在 DEV 的机器上将行结尾设置为 CR(而不是 CRLF),则在推送期间它们不会转换为 LF 并且将保持 CR 状态GIT 存储库。彻底检查行尾

In our project we declared necessity of setting autocrlf to input value. Here is why you may be interested in this:

  • Your project runs in Linux/Unix environment and developers are writing code in Windows. During push CRLF is transformed into LF automatically, thus you just need to checkout Git repo on Unix server and everything will be good out of the box.
  • Sometimes developers upload configuration from Windows machines to servers manually using WinSCP. This leads to CRLF line endings on Unix machines and failed app starts. "input" value partially eliminates the number of such cases

The issues we faced not long ago:

  • If the line endings are set to CR (not CRLF) on DEV's machine, during push they won't be transformed to LF and will stay CR in GIT repository. Do checks of line-endings thoroughly
霊感 2024-09-08 09:08:21

为我。

编辑 .gitattributes 文件。

添加

*.dll binary

然后一切顺利。

For me.

Edit .gitattributes file.

add

*.dll binary

Then everything goes well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文