GIT 抱怨在切换分支时随机更改文件



Start from

git status
# On branch master
nothing to commit, working directory clean

然后切换到另一个分支

git checkout somebranch

切换到新分支并运行git status Git 有时会抱怨随机更改的文件。如果我用git diff比较文件,它将首先显示整个文件,删除所有行,然后再次添加

- someline
- someotherline
+ someline
+ someotherline

然后运行git diff --ignore-space-at-eol .不会显示任何文件已更改,导致我相信 git 存储库中的某个地方存在行尾问题,因为如果我使用我选择的合并工具(Beyond Compare(对文件进行二进制比较,它会告诉我文件是二进制相同的,即使 git 抱怨它们不同, 地狱我什至做了一个十六进制比较,它们确实相同,那么为什么 git 将它们视为已更改?

该存储库位于旧的 svn 存储库上,该存储库按照 github 指南进行了转换 https://help.github.com/articles/importing-from-subversion 之后,我们将 .gitattributes 文件添加到解决方案中,如下所示:

# .gitattributes
# Auto detect text files and perform LF normalization
* text=auto
# Custom for Visual Studio
*.cs     diff=csharp
*.sln    merge=union
*.csproj merge=union
*.vbproj merge=union
*.fsproj merge=union
*.dbproj merge=union
# Standard to msysgit
*.doc    diff=astextplain
*.DOC    diff=astextplain
*.docx diff=astextplain
*.DOCX diff=astextplain
*.dot  diff=astextplain
*.DOT  diff=astextplain
*.pdf  diff=astextplain
*.PDF    diff=astextplain
*.rtf    diff=astextplain
*.RTF    diff=astextplain

添加 .gitattributes 文件后,我们还遵循 githubs 指南来修复 https://help.github.com/articles/dealing-with-line-endings

团队中的每个人都

在Windows上运行,团队中的每个人都在使用core.autocrlf=true,并且每个人都在使用至少

git --version
git version 1.8.3.msysgit.0

这里可能出了什么问题?git 抱怨的文件是完全随机的,它发生在团队中的每个人身上。此外,不可能使用尚未真正更改的 git 签出文件来还原它抱怨的文件。

在我的

仓库中添加Visual Studio生成的默认.gitattributes文件后,我遇到了同样的问题。您可以通过注释掉 .gitattributes 中的以下行来修复它:

# * text=auto

仅提交此文件,所有其他虚假更改的文件现在将从本地更改列表中消失。

注意:auto 选项指示 git 存储内部以 LF 行结尾的所有文件。当您签出文件时,行尾将转换回 CRLF,因此我们然后运行 git diff,它会看到来自仓库的 LF 和来自签出版本的 CRLF 之间的差异。对我来说似乎是一个错误。

首先,不要混合旧方案(core.autocrlf(和新方案(.gitattributes(。

要在本地修复有问题的文件,您可以执行以下操作:

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

最新更新