在使用“text”属性规范化文件后,如何强制 git 签出 master 分支并删除回车符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17223527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 16:24:39  来源:igfitidea点击:

How do I force git to checkout the master branch and remove carriage returns after I've normalized files using the "text" attribute?

gitnewlinecore.autocrlf

提问by Jason

Okay, so I added the file .gitattributeswith lines like this

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

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

I then followed the instructions at http://git-scm.com/docs/gitattributes#_checking-out_and_checking-in

然后我按照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"

But now my working copy still has the carriage returns! I have untracked files that I would like to keep. How do I have git checkout the master branch again with the normalized files?

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

I know the files are normalized in the repository because when I clone the repo, I have all the files without the carriage returns.

我知道这些文件在存储库中已规范化,因为当我克隆存储库时,我拥有所有没有回车的文件。

回答by Jason

Ahah! Checkout the previous commit, then checkout the master.

啊啊!签出之前的提交,然后签出主。

git checkout HEAD^
git checkout -f master

回答by mechsin

As others have pointed out one could just delete all the files in the repo and then check them out. I prefer this method and it can be done with the code below

正如其他人指出的那样,可以删除存储库中的所有文件,然后将它们检出。我更喜欢这种方法,它可以用下面的代码来完成

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

or one line

或一行

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

I use it all the time and haven't found any down sides yet!

我一直在使用它,但还没有发现任何缺点!

For some further explanation, the -zappends a null character onto the end of each entry output by ls-files, and the -0tells xargsto delimit the output it was receiving by those null characters.

为了进一步解释,-z将一个空字符附加到每个条目输出的末尾ls-files,并-0告诉xargs用这些空字符分隔它接收的输出。