我克隆了一个行尾不一致的存储库。我添加了一个 .gitattributes
来设置我想要规范化的文件的文本属性。现在,当我提交更改时,我会收到消息:
warning: CRLF will be replaced by LF in FILE.
The file will have its original line endings in your working directory.
如何让 git 为我规范化我的文件工作副本?最好我希望 git 规范化整个工作树。
最佳答案
对于使用 v2.16 或更高版本的用户,您可以简单地使用:
git add --renormalize . # Update index with renormalized files
git status # Show the files that will be normalized
git commit -m "Introduce end-of-line normalization"
这些方向直接来自 gitattributes .对于旧版本,docs (在 v2.12 之前)提供不同的答案:
rm .git/index # Remove the index to force git to
git reset # re-scan the working directory
git status # Show files that will be normalized
git add -u
git add .gitattributes
git commit -m "Introduce end-of-line normalization"
在编辑 .gitattributes
后执行此序列。
更新
似乎有些用户在使用上述说明时遇到了问题。更新了 gitattributes 的文档(2.12 到 2.14)显示一组新指令(编辑 .gitattributes 文件后):
git read-tree --empty # Clean index, force re-scan of working directory
git add .
git status # Show files that will be normalized
git commit -m "Introduce end-of-line normalization"
感谢@vossad01感谢您指出这一点。
此外,无论使用哪种解决方案,工作副本中的文件仍保留其旧的行尾。如果您想更新它们,请确保您的工作树是干净的并使用:
git rm --cached -r .
git reset --hard
现在行尾在您的工作树中将是正确的。
关于git - 如何规范 Git 中的工作树行结尾?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15641259/