git `.gitattributes` 文件中 `text=auto` 的目的是什么?

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

What is the purpose of `text=auto` in `.gitattributes` file?

gitgitattributes

提问by Fizer Khan

Mostly .gitattributesfile has * text=auto. What is the purpose of text=autoin that file?

大多数.gitattributes文件都有* text=auto. text=auto那个文件的目的是什么?

回答by Flimm

From the docs:

文档

Each line in .gitattributes(or .git/info/attributes) file is of form:

pattern attr1 attr2 ...

.gitattributes(或.git/info/attributes) 文件中的每一行都是以下形式:

pattern attr1 attr2 ...

So here, the pattern is *, which means all files, and the attribute is text=auto.

所以在这里,模式是*,表示所有文件,属性是text=auto

What does text=autodo? From the documentation:

有什么作用text=auto?从文档:

When text is set to "auto", the path is marked for automatic end-of-line normalization. If Git decides that the content is text, its line endings are normalized to LF on checkin.

当 text 设置为“auto”时,路径被标记为自动行尾规范化。如果 Git 决定内容是文本,则在签入时将其行尾规范化为 LF。

What's the default behaviour if it's not enabled?

如果未启用,默认行为是什么?

Unspecified

If the text attribute is unspecified, Git uses the core.autocrlf configuration variable to determine if the file should be converted.

未指定

如果未指定 text 属性,Git 将使用 core.autocrlf 配置变量来确定是否应转换文件。

What does core.autocrlfdo? From the docs:

有什么作用core.autocrlf?从文档:

   core.autocrlf

Setting this variable to "true" is almost the same as setting the text attribute to "auto" on all files except that text files are not guaranteed to be normalized: files that contain CRLF in the repository will not be touched. Use this setting if you want to have CRLF line endings in your working directory even though the repository does not have normalized line endings. This variable can be set to input, in which case no output conversion is performed.

   core.autocrlf

将此变量设置为“true”几乎与在所有文件上将文本属性设置为“auto”相同,只是不能保证文本文件被规范化:不会触及存储库中包含 CRLF 的文件。如果您希望在工作目录中有 CRLF 行结尾,即使存储库没有规范化的行结尾,也可以使用此设置。该变量可以设置为输入,在这种情况下不执行输出转换。

If you think this all as clear as mud, you're not alone.

如果您认为这一切都像泥巴一样清晰,那么您并不孤单。

Here's what * text=autodoes in my words:when someone commits a file, Git guesses whether that file is a text file or not, and if it is, it will commit a version of the file where all CR + LF bytes are replaced with LF bytes. It doesn't directly affect what files look like in the working tree, there are other settings that will convert LF bytes to CR + LF bytes when checking out a file.

下面是* text=auto我的话呢:当有人提交了一个文件,Git的猜测文件是否是一个文本文件或没有,如果是,它会提交文件的版本,所有CR + LF字节与LF字节替换。它不会直接影响文件在工作树中的外观,还有其他设置可以在检出文件时将 LF 字节转换为 CR + LF 字节。

Recommendation:

推荐:

I would notrecommend putting * text=autoin the .gitattributesfile. Instead, I would recommend something like this:

建议把* text=auto.gitattributes文件。相反,我会推荐这样的东西:

*.txt text
*.html text
*.css text
*.js text

This explicitly designates which files are text files, which get CRLF converted to LF in the object database (but not necessarily in the working tree). We had a repo with * text=auto, and Git guessed wrong for an image file that it was a text file, causing it to corrupt it as it replaced CR + LF bytes with LF bytes in the object database. That was not a fun one to debug.

这明确指定哪些文件是文本文件,在对象数据库中(但不一定在工作树中)将 CRLF 转换为 LF。我们有一个带有 的存储库* text=auto,Git 猜错了一个图像文件,认为它是一个文本文件,导致它损坏它,因为它用对象数据库中的 LF 字节替换了 CR + LF 字节。这不是一个有趣的调试。

If you must use * text=auto, put it as the first line in .gitattributes, so that the later lines can override it. This seems to be becoming an increasingly popular practise.

如果必须使用* text=auto,请将其作为 中的第一行.gitattributes,以便后面的行可以覆盖它。这似乎正在成为一种越来越流行的做法。

回答by Dave Zych

It ensures line endings are normalized. Source: Kernel.org

它确保行尾标准化。资料来源:Kernel.org

When text is set to "auto", the path is marked for automatic end-of-line normalization. If git decides that the content is text, its line endings are normalized to LF on checkin.

If you want to interoperate with a source code management system that enforces end-of-line normalization, or you simply want all text files in your repository to be normalized, you should instead set the text attribute to "auto" for all files.

This ensures that all files that git considers to be text will have normalized (LF) line endings in the repository.

当 text 设置为“auto”时,路径被标记为自动行尾规范化。如果 git 决定内容是文本,则在签入时将其行尾规范化为 LF。

如果您想与强制行尾规范化的源代码管理系统进行互操作,或者您只是希望对存储库中的所有文本文件进行规范化,则应将所有文件的文本属性设置为“自动”。

这确保 git 认为是文本的所有文件都将在存储库中具有规范化 (LF) 行结尾。

回答by Karl Z?ller

That configuration is with regard to how line endings are handled. When enabled, all line endings are converted to LF in the repository. There are other flags to deal with how line endings are converted in your working directory. Full info on the issue us here: https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html

该配置与如何处理行尾有关。启用后,所有行结尾都将转换为存储库中的 LF。还有其他标志可以处理如何在您的工作目录中转换行尾。关于我们这里问题的完整信息:https: //www.kernel.org/pub/software/scm/git/docs/gitattributes.html