首页 > 解决方案 > core.eol = crlf 在添加时而不是在结帐时设置 crlfs

问题描述

根据有关文件core-eol

为标记为文本的文件设置要在工作目录中使用的行结束类型(通过设置 text 属性,或者通过设置 text=auto 和 Git 将内容自动检测为文本)。

所以问题 #1 是:core-eol 是否可以在没有 .gitattributes 文件的情况下工作?

问题 #2 是用于签出到工作目录还是工作目录签入到索引?或两者?

使用 git for windows 2.33.1 和全局设置(无本地)core.autocrlf = false(因此它不会覆盖 core.eol)core.eol = crlf并运行 Git Bash(回显命令 > 导致 eol=LF)

1. git config --global core.autocrlf false
2. git config --global core.eol crlf
3. mkdir testEOL
4. cd testEOL
5. echo Hello > hello.txt // file has LF eol
6. git init
7. git add . // file still has LF eol in working directory
8. git commit -m "Initial commit"
9. echo "* text" > .gitattributes
10. git add . // file has LF eol, however -> warning: LF will be replaced by CRLF in .gitattributes.
11. git commit -m "Git attributes"
12. echo New Line >> hello.txt
13. git add . // warning: LF will be replaced by CRLF in hello.txt.
14. git commit -m "Change to hello"
14a. git rm hello.txt
15. git reset --hard HEAD // EDIT: hello.txt has CRLF

这些问题的答案似乎是:

  1. 否(第 7 行)
  2. 签入和签出(第 10,13 和 15 行)- 删除文件后(见更新的 14a)

这只是我的实验还是这是真的?

标签: git

解决方案


该文档指出,core.eol仅影响具有text属性集的文件。如果未设置该属性,Git 会查看core.autocrlf以确定它,并设置将覆盖该core.eol值。因此,如果要控制行尾,则需要使用.gitattributes文件来设置属性。

core.eol选项和eol属性只影响工作树中的内容。任何具有该text属性的文件在存储在存储库本身时都会将其行尾转换为 LF,然后该属性或设置将确定它在工作树中具有哪些行尾。

您看到的警告是告诉您工作树中的文件具有 LF(因为您使用 Unix shell 工具创建了它),但是您对工作树的设置要求它在工作树中具有 CRLF。因此,您在未来结账时可能会遇到与现在不同的行为。

执行 agit add不会将工作树中的文件更改为具有指定的结尾,只会将其添加到具有 LF 结尾的存储库中。如果要更改工作树中的行尾,则需要强制再次签出文件,您已经完成了git reset --hard.


推荐阅读