Git检测到实际上是字节相同的文件

我有一个问题(通过龟的git),显示我的项目的一些文件被修改,但他们实际上没有修改。 我已经通过重新创build仓库克隆来重复检查,并且在不触及它的情况下,我已经在新创build的工作副本中检测到“修改”的文件。 这是烦人的,因为一些操作被阻止(因为这将覆盖我的“修改”的文件),但我不能恢复他们,删除+恢复不起作用。 承诺“改变”的作品,但它不是一个理想的解决scheme。

我在Windows上,使用TortoiseGit 1.8.16.0和Git 2.6.4。 直接使用git status也显示相同的文件被“修改”。

这似乎只发生在我的项目,曾经是一个子模块的目录,但我现在使用git子树。 所以在某个时候,我完全删除了子模块(或者我认为),并创build了一个子树。

有没有人有同样的问题? 我怎么能一劳永逸呢? (甚至在承诺“变更”之后,一段时间以后,有时几个星期之后,我还会有其他文件,或者有时是相同的文件,开始显示相同的奇怪问题)。

下面是这些文件之一的diff的结果: git diff app.config

 diff --git a/Ozytis.Common/Web/app.config b/Ozytis.Common/Web/app.config index 3686aab..f559fe7 100644 --- a/Ozytis.Common/Web/app.config +++ b/Ozytis.Common/Web/app.config @@ -1,25 +1,25 @@ -<U+FEFF><?xml version="1.0" encoding="utf-8"?> -<configuration> - <configSections> - <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> - <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=ne utral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> - </configSections> - <runtime> - <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> - <dependentAssembly> - <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" /> - <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" /> - </dependentAssembly> - </assemblyBinding> - </runtime> - <entityFramework> - <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> - <parameters> - <parameter value="v11.0" /> - </parameters> - </defaultConnectionFactory> - <providers> - <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> - </providers> - </entityFramework> +<U+FEFF><?xml version="1.0" encoding="utf-8"?> +<configuration> + <configSections> + <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> + <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=ne utral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> + </configSections> + <runtime> + <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> + <dependentAssembly> + <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" /> + <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" /> + </dependentAssembly> + </assemblyBinding> + </runtime> + <entityFramework> + <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> + <parameters> + <parameter value="v11.0" /> + </parameters> + </defaultConnectionFactory> + <providers> + <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> + </providers> + </entityFramework> </configuration> \ No newline at end of file 

对于这个文件,diff显示所有的行被修改。 不过,我用一个hex编辑器检查了文件的前一版本和当前版本,换行是一样的,实际上每一个字节都是一样的。

此外,有些文件显示只有几行不同(但是,他们不是)。 即使整个文件具有一致的换行符。

这似乎确实是一个行结束的问题(感谢user3159253和HBHB的评论)。

看起来像一个混帐错误,因为它为什么报告文件是不同的,但同时,当你在你的机器上它们改变行结束,所以你不能看到差异在哪里? 我相信它不应该显示行结束差异(不是与git status而不是与git diff ),如果它被配置为无论如何改变它们。

此外,我已经尝试改变core.autocrlf之前问我的问题,但事实证明我的项目包含一个.gitattributes,这是真正的问题 。 该文件以* text开始,然后将几个文件格式更改为二进制文件,并且优先于core.autocrlf。 由于每个人都在为这个项目在Windows上工作, 所以我把它改成* binary (至少现在看起来很清楚)。 这使git检测到更多的文件更改(再次,git是不一致的呢?),但这次我真的看到行结束差异。 我承诺他们,我希望这是故事的结尾。

更新:

在.gitattributes中使用* binary不起作用,因为现在git不能合并文本文件,因为他认为它们是二进制文件。 正确的版本(如果你想防止改变行结束的git,但仍然正确地合并文本文件)是* -text

取消设置路径上的文本属性会告诉Git在签入或签出时不要尝试任何行尾转换。

http://git-scm.com/docs/gitattributes

这个解决方案可能并不理想,因为在刷新工作副本之后,所有的文件都会有unix样式的行结尾(因为这是git所存储的)。 所以你必须把所有的文件都转换回windows的结尾( dos2unix帮助),并做出一个大的提交,这将是未来合并(如果有的话)的痛苦。