windows Git 使所有检出文件的行尾 CRLF

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

Git makes all checked out files' end of line CRLF

windowslinuxgitmacos

提问by Antonin

I am programming on mac, and I don't really understand what Git does with the end of line of my files :

我在 mac 上编程,我真的不明白 Git 对我的文件行尾做了什么:

I created a repository with some files in Unix format (LF end of line).

我创建了一个存储库,其中包含一些 Unix 格式的文件(LF 行尾)

When I clone the repositorythat I created, all my end of lines are CRLF. Shouldn't it detect automatically that I need LF end of line ?

当我克隆我创建的存储库时,我所有的行尾都是 CRLF。它不应该自动检测到我需要 LF 行尾吗?

I have autoclrfset to true.

我已将autoclrf设置为true。

GIT's documentation about autoclrf is pretty hard to understand :

GIT 关于 autoclrf 的文档很难理解:

If you simply want to have CRLF line endings in your working directory regardless of the repository you are working with, you can set the config variable "core.autocrlf" without changing any attributes.

[core]

   autocrlf = true

This does not force normalization of all text files, but does ensure that text files that you introduce to the repository have their line endings normalized to LF when they are added, and that files that are already normalized in the repository stay normalized.

如果您只想在您的工作目录中使用 CRLF 行结尾,而不管您使用的是哪个存储库,您可以设置配置变量“core.autocrlf”而无需更改任何属性。

[核]

   autocrlf = true

这不会强制对所有文本文件进行规范化,但会确保您引入存储库的文本文件在添加时将其行尾规范化为 LF,并且已在存储库中规范化的文件保持规范化。

The first sentence says "if you want to have all crlf", when the second sentence says that git will auto-adjust the end of lines.

第一句说“如果你想要所有的 crlf”,第二句说 git 会自动调整行尾。

In my case, it seems like Git converts everything to CRLF and leaves it like that when I try to clone.

就我而言,当我尝试克隆时,Git 似乎将所有内容都转换为 CRLF 并保持原样。

采纳答案by Justin Turner Arthur

The gitattributes manpage is poorly laid out. In a later section, you'll find:

gitattributes 联机帮助页布局不佳。在后面的部分中,您会发现:

The core.eolconfiguration variable controls which line endings git will use for normalized files in your working directory; the default is to use the native line ending for your platform, or CRLF if core.autocrlfis set.

core.eol配置变量控制该行git会使用在你的工作目录标准化文件的结局; 默认是使用平台的本机行结尾,如果设置了则使用CRLF core.autocrlf

So, unless you've specified core.eol, you'll end up with lines terminated by CR+LF characters regardless of whether you're using Apple Mac OS X, Microsoft Windows, or Ubuntu Linux.

因此,除非您已指定 ,否则core.eol无论您使用的是 Apple Mac OS X、Microsoft Windows 还是 Ubuntu Linux,您最终都会以 CR+LF 字符结尾。

From your question:

从你的问题:

The first sentence says "if you want to have all crlf", when the second sentence says that git will auto-adjust the end of lines.

第一句说“如果你想要所有的 crlf”,第二句说 git 会自动调整行尾。

It's important to note that there are two directions of adjustment that get performed when core.autocrlfis set to true:

重要的是要注意,当core.autocrlf设置为时,会执行两个调整方向true

  • CR+LFs will become LFs in your repository/repositories.That is to say, the commit files and repository back-end will have LFs at the end of lines in text files. This keeps things consistent in your commit history, making diffs/comparisons easier if one of your coworkers' IDEs decides to magically convert your LFs to CR+LFs (who wants to see that in their diff?). You'll save a few bytes of hard drive space as well, I suppose.
  • LFs will become CR+LFs in your working directory.In your checked out file system, any new text file will have lines ending in CR+LF once git touches it. This will happen even if the file had lines ending in plain LFs when you first created it.
  • CR+LFs 将成为您的repository/repositories 中的LFs 。也就是说,提交文件和存储库后端将在文本文件的行尾处有 LF。这使您的提交历史记录保持一致,如果您同事的一个 IDE 决定神奇地将您的 LF 转换为 CR+LF(谁想在他们的差异中看到?),则可以更轻松地进行差异/比较。我想你也会节省几个字节的硬盘空间。
  • LFs 将成为您工作目录中的CR+LFs 。在您签出的文件系统中,一旦 git 接触到任何新的文本文件,它都会有以 CR+LF 结尾的行。即使在您第一次创建文件时文件的行以普通 LF 结尾,也会发生这种情况。

The first thing you'll want to do is to unset core.autocrlfor set it to false. If you then want checked out text files to conform to the user's OS-preferred line endings, regardless of how they were created, just add this to your .gitattributes:

您要做的第一件事是取消设置core.autocrlf或将其设置为false. 如果您希望签出的文本文件符合用户的操作系统首选行尾,无论它们是如何创建的,只需将其添加到您的 .gitattributes 中:

* text=auto

Alternatively, if git's not good at guessing which of your files are text, you could declare a specific extension to undergo this two-way normalization:

或者,如果 git 不擅长猜测您的哪些文件是文本,您可以声明一个特定的扩展名来进行这种双向规范化:

*.ext text

(where extis the file extension in question)

(有ext问题的文件扩展名在哪里)

回答by mipadi

When you set core.autocrlfto true, Git converts line endings to LFwhen committing to the repo, but writes out files to the working tree using the line endings appropriate for your platform settings (LFon Mac/Unix/Linux, CRLFon Windows).

当您设置core.autocrlf为 时true,Git 将行尾转换为LF提交到 repo 时,但使用适合您的平台设置(LF在 Mac/Unix/Linux 上,CRLF在 Windows 上)的行尾将文件写到工作树中。

回答by Hazok

Yes, you should see CRLF in your working tree on Windows when autocrlf is set to true, which includes if you're browsing through the explorer. If you push to a Linux repo, you should see that all your files end correctly with LF.

是的,当 autocrlf 设置为 true 时,您应该在 Windows 上的工作树中看到 CRLF,这包括您是否正在浏览资源管理器。如果推送到 Linux 存储库,您应该会看到所有文件都以 LF 正确结尾。