为什么git将一些cpp文件视为二进制文件?

sti*_*ijn 5 git

这里的输出git log:

* 5a831fdb34f05edd62321d1193a96b8f96486d69      HEAD (HEAD, origin/work, work)
|  LIB/xxx.cpp                        |  Bin 592994 -> 593572 bytes
|  LIB/xxx.h                          |    5 +++++
|  LIB/bbb/xxx.h                      |    9 +++++++++
|  LIB/aaa/xxx.cpp                    |  Bin 321534 -> 321536 bytes
|  LIB/aaa/yyy.cpp                    |   31 +++++++------------------------
|  tests/aaa/xxx.cpp                  |   29 +++++++++++++++++++++++++++++
|  tests/test_xxx.vcproj              |    4 ++++
|  7 files changed, 54 insertions(+), 24 deletions(-)
Run Code Online (Sandbox Code Playgroud)

为什么它将某些文件视为二进制文件,而其他文件则不是?这给出了严重的问题,因为git也不想自动合并它们.因此几乎所有的merge/rebase/pull动作都变得很痛苦.

这是repo配置:

[core]
  repositoryformatversion = 0
  filemode = false
  bare = false
  logallrefupdates = true
  symlinks = false
  ignorecase = true
  hideDotFiles = dotGitOnly
[remote "origin"]
  fetch = +refs/heads/*:refs/remotes/origin/*
  url = https://xxx/project.git
[branch "master"]
  remote = origin
  merge = refs/heads/master
[branch "work"]
  remote = origin
  merge = refs/heads/work
[svn-remote "svn"]
  url = xxxx
  fetch = :refs/remotes/git-svn
Run Code Online (Sandbox Code Playgroud)

主.gitconfig中的core.autocrlf = false也是如此.

编辑 我按照评论中的建议将core.autocrlf设置为true,但这似乎不会影响我之后的下一次合并(也许现在改变autocrlf已经太晚了?或者它与问题无关?):

> git merge work
warning: Cannot merge binary files: LIB/xxx.cpp (HEAD vs. work)

warning: Cannot merge binary files: LIB/aaa/xxx.cpp (HEAD vs. work)

Auto-merging LIB/xxx.cpp
CONFLICT (content): Merge conflict in LLIB/xxx.cpp
Auto-merging LIB/xxx.h
Auto-merging LIB/aaa/xxx.cpp
CONFLICT (content): Merge conflict in LIB/aaa/xxx.cpp
Automatic merge failed; fix conflicts and then commit the result.
Run Code Online (Sandbox Code Playgroud)

此外,现在gits坚持改变几个文件中的lineendings(这是我想要的).

我在属性文件中最终得到了这个,多亏了:

*.cpp -text crlf diff
Run Code Online (Sandbox Code Playgroud)

-text即使core.autocrlf设置为false,也需要使用该部分来保持git不转换lineends.

uli*_*tko 10

尝试将以下行添加到您的$repo/.git/info/attributes:

*.cpp crlf diff
Run Code Online (Sandbox Code Playgroud)

您可以在gitattributes系统或用户范围内指定它.

  • 它也可能是编码问题,Git最适合使用utf-8,如果编码类似于utf-16,Git会认为它是二进制的,无论你在`.gitattributes`中设置什么. (3认同)

小智 5

它将某些文件视为二进制文件,因为它们的文件编码错误.如果将这些文件转换为UTF-8(或与普通文件中的编码相同),它应该可以正常工作.要更改文件编码,请使用notepad ++或任何其他方式.