git - 如何规范 Git 中的工作树行结尾?

标签 git line-endings gitattributes

我克隆了一个行尾不一致的存储库。我添加了一个 .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/

相关文章:

git - 使用 .gitattributes 在所有平台上的 git repo 中强制 LF eol

git - 从 Git 中提取失败并给我以下错误 : client_global_hostkeys_private_confirm: server gave bad signature for RSA key 0

git - 如何为github中两个分支之间的单个文件生成差异

windows - 混合环境下git crlf配置

linux - 带有忽略空格/行尾的樱桃选择

windows - 处理 Git 拒绝重置的文件?

github - 使用 gitattributes 作为语言学家示例

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

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

git - 有没有办法使用git查看文件的所有版本?