git - `text=auto` 文件中 `.gitattributes` 的用途是什么?

标签 git gitattributes

大多数 .gitattributes 文件都有 * text=auto。该文件中 text=auto 的用途是什么?

最佳答案

来自 the docs :

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

pattern attr1 attr2 ...

所以这里的pattern是*,表示所有文件,属性是text=auto

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.

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

Unspecified

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

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.

如果您认为这一切都一清二楚,那么您并不孤单。

* text=auto 用我的话来说就是这样做的:当有人提交文件时,Git 会猜测该文件是否是文本文件,如果是, 它将提交所有 CR + LF bytes 所在的文件版本被替换为 LF 字节。它不会直接影响文件在工作树中的样子,还有其他设置可以在 checkout 文件时将 LF 字节转换为 CR + LF 字节。

建议:

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

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

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

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

关于git - `text=auto` 文件中 `.gitattributes` 的用途是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21472971/

相关文章:

Git merge 到之前 merge 的分支

git 将目录移动到另一个存储库,同时保留历史记录

Eclipse 和 EGit : How to easily review changes to ALL modified files before committing to *local* repository

git - 向 .gitattributes 添加评论/备注

git - diff和diff = astextplain有什么区别?

git - 如何列出 git 存储库中跟踪文件的所有不同扩展名?

git - 具有 ssh 访问 bitbucket : Permission denied (publickey). fatal error 的 Jenkins git 插件:无法从远程存储库读取

git - 分配 git 分支名称不足以撤消分离的头吗?

gitignore 和 gitattributes 优先级

git - Hook 还是不 Hook - git