首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在Git中规范化工作的树线结尾?

如何在Git中规范化工作的树线结尾?
EN

Stack Overflow用户
提问于 2013-03-26 23:42:30
回答 7查看 46.2K关注 0票数 89

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

代码语言:javascript
运行
复制
warning: CRLF will be replaced by LF in FILE.
The file will have its original line endings in your working directory.

我怎样才能让git为我规范化我的文件的工作副本?最好是让git对整个工作树进行规范化。

EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2018-06-01 21:54:57

在Git client 2.16和更高版本中,现在有一种更简单的方法来实现这一点。只需使用:

代码语言:javascript
运行
复制
git add --renormalize .

注意:最好是在一个干净的工作区中这样做。有关详情,请参阅:

票数 103
EN

Stack Overflow用户

发布于 2013-03-27 04:30:34

对于使用v2.16或更高版本的用户,您可以简单地使用:

代码语言:javascript
运行
复制
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之前的版本)提供了不同的答案:

代码语言:javascript
运行
复制
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文件之后):

代码语言:javascript
运行
复制
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指出这一点。

此外,无论使用哪种解决方案,工作副本中的文件仍然保留其旧的行尾。如果您想要更新它们,请确保您的工作树是干净的,并使用:

代码语言:javascript
运行
复制
git rm --cached -r .
git reset --hard

现在,您的工作树中的行尾将是正确的。

票数 109
EN

Stack Overflow用户

发布于 2017-08-07 20:11:40

另一种方法(仅在使用的命令方面不同)

确保存储库中没有任何挂起的更改:

代码语言:javascript
运行
复制
$ git status
$ git stash

修改.gitattributes,以便更改CRLF解释:

代码语言:javascript
运行
复制
$ echo "*.txt  text" >>.gitattributes
$ git commit -m "Made .txt files a subject to CRLF normalization." -- .gitattributes

从索引中删除数据并刷新工作目录:

代码语言:javascript
运行
复制
$ git rm --cached -r .
$ git reset --hard

回顾Git提出的CRLF修复:

代码语言:javascript
运行
复制
$ git ls-files --eol
$ git status
$ git diff

同意Git的决定:

代码语言:javascript
运行
复制
$ git add -u
$ git commit -m "Normalized CRLF for .txt files"

重新加载更改,就像已完成干净克隆一样:

代码语言:javascript
运行
复制
$ git rm --cached -r .
$ git reset --hard
票数 11
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15641259

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档