我已经克隆了一个行尾不一致的存储库。我已经添加了一个.gitattributes
,它为我想要规范化的文件设置文本属性。现在,当我提交更改时,我会收到以下消息:
warning: CRLF will be replaced by LF in FILE.
The file will have its original line endings in your working directory.
我怎样才能让git为我规范化我的文件的工作副本?最好是让git对整个工作树进行规范化。
发布于 2018-06-01 21:54:57
在Git client 2.16和更高版本中,现在有一种更简单的方法来实现这一点。只需使用:
git add --renormalize .
注意:最好是在一个干净的工作区中这样做。有关详情,请参阅:
发布于 2013-03-27 04:30:34
对于使用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 (2.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
现在,您的工作树中的行尾将是正确的。
发布于 2017-08-07 20:11:40
另一种方法(仅在使用的命令方面不同)
确保存储库中没有任何挂起的更改:
$ git status
$ git stash
修改.gitattributes
,以便更改CRLF解释:
$ echo "*.txt text" >>.gitattributes
$ git commit -m "Made .txt files a subject to CRLF normalization." -- .gitattributes
从索引中删除数据并刷新工作目录:
$ git rm --cached -r .
$ git reset --hard
回顾Git提出的CRLF修复:
$ git ls-files --eol
$ git status
$ git diff
同意Git的决定:
$ git add -u
$ git commit -m "Normalized CRLF for .txt files"
重新加载更改,就像已完成干净克隆一样:
$ git rm --cached -r .
$ git reset --hard
https://stackoverflow.com/questions/15641259
复制相似问题