如何正常化Git中的工作树行结尾? [英] How to normalize working tree line endings in Git?

查看:112
本文介绍了如何正常化Git中的工作树行结尾?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我克隆了一个存在不一致行结束的存储库。我添加了一个 .gitattributes ,它为我想规范化的文件设置文本属性。现在,当我提交更改时,我收到以下消息:

 警告:CRLF将在FILE中由LF替换。 
该文件将在工作目录中具有其原始行结尾。

如何让git对我的文件工作副本进行归一化处理?最好我希望git规范化整个工作树。 gitattributes 提供了答案:

  rm .git / index#删除索引以强制git到
git reset#重新扫描工作目录
git status#显示将被标准化的文件
git add -u
git add .gitattributes
git commit -m引入行尾标准化

编辑 .gitattributes 后执行此操作。



更新



看起来有些用户在上述说明中遇到了麻烦。 gitattributes的更新文档显示了一套新的说明(编辑.gitattributes文件后):

  git read-tree --empty #清理索引,强制重新扫描工作目录
git add。
git status#显示将被标准化的文件
git commit -m引入行尾标准化

感谢 @ vossad01 指出了这一点。



另外,对于任一解决方案,工作副本中的文件仍保留其旧的行结尾。如果您想更新它们,请确保您的工作树很干净并使用:

  git rm --cached -r。 
git reset --hard

现在行结束符在您的工作树中是正确的。

I have cloned a repository that had inconsistend line endings. I have added a .gitattributes that sets the text attribute for the files I want to normalize. Now when I commit changes I get the message:

warning: CRLF will be replaced by LF in FILE.
The file will have its original line endings in your working directory.

How can I make git normalize my working copy of the file for me? Preferably I would like git to normalize the entire working tree.

解决方案

The docs for gitattributes provides the answer:

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"

Do this sequence after you have edited .gitattributes.

Update

It appears some users have had trouble with the above instructions. Updated docs for gitattributes shows a new set of instructions (after editing the .gitattributes files):

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"

Thanks to @vossad01 for pointing this out.

Also, with either solution the files in your working copy still retain their old line endings. If you want to update them, make sure your working tree is clean and use:

git rm --cached -r .
git reset --hard

Now the line endings will be correct in your working tree.

这篇关于如何正常化Git中的工作树行结尾?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆