在使用“text”属性对文件进行规范化后,如何强制git检出主分支并删除回车?

时间:2013-06-20 20:51:03

标签: git newline core.autocrlf

好的,所以我添加了文件.gitattributes这样的行

*.css text
*.js text
etc...

然后我按照http://git-scm.com/docs/gitattributes#_checking-out_and_checking-in

上的说明操作
$ 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"

但是现在我的工作副本仍然有回车!我有未跟踪的文件,我想保留。如何使用规范化文件再次git checkout master分支?

我知道文件在存储库中已标准化,因为当我克隆repo时,我拥有所有没有回车符的文件。

2 个答案:

答案 0 :(得分:226)

AHAH!签出上一次提交,然后签出主。

git checkout HEAD^
git checkout -f master

答案 1 :(得分:13)

正如其他人指出的那样,可以删除仓库中的所有文件,然后检查出来。我更喜欢这种方法,可以使用下面的代码

git ls-files -z | xargs -0 rm
git checkout -- .

或一行

git ls-files -z | xargs -0 rm ; git checkout -- .

我一直都在使用它,但还没有找到任何缺点!

对于某些进一步的解释,-zls-files的每个条目输出的末尾附加一个空字符,-0告诉xargs分隔输出它正在接收这些空字符。