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

Jas*_*son 89 git newline core.autocrlf

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

*.css text
*.js text
etc...
Run Code Online (Sandbox Code Playgroud)

然后我按照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"
Run Code Online (Sandbox Code Playgroud)

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

我知道文件在存储库中是规范化的,因为当我克隆repo时,我有所有没有回车符的文件.

Jas*_*son 244

啊啊!签出上一次提交,然后签出主人.

git checkout HEAD^
git checkout -f master
Run Code Online (Sandbox Code Playgroud)

  • 感谢您的解决方法,但是在git中明显的问题是"checkout -f"并没有真正强制重新结账.另一个问题是首先删除所有工作副本文件(即除了.git目录之外的所有文件). (5认同)
  • 这实际上是行不通的。GIT将仅更新两次提交之间更改的文件(有一些例外)。如果存储库是全新的,例如。只有两次提交,而第一次恰好为空,则此解决方案将起作用。否则,您需要按照Mechsin的回答所述强制删除所有文件。 (2认同)

mec*_*sin 13

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

git ls-files -z | xargs -0 rm
git checkout -- .
Run Code Online (Sandbox Code Playgroud)

或一行

git ls-files -z | xargs -0 rm ; git checkout -- .
Run Code Online (Sandbox Code Playgroud)

我一直都在使用它,还没有发现任何不足之处!

为了进一步解释,在-z每个条目输出的末尾附加一个空字符ls-files,并-0告诉xargs它分隔它们由那些空字符接收的输出.