why does git treat some cpp files as binary?

2020-03-03 07:40发布

here's output of 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(-)

why is it treating some files as binary, and others not? This gives serious problems since git also doesn't want to automatically merge them.. Hence pretty much all merge/rebase/pull actions become a pain.

Here's the repo config:

[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

also core.autocrlf = false in the main .gitconfig.

edit I set core.autocrlf to true as suggested in the comments, but this doesn't seem to affect the next merge I'm after (maybe it's too late now to change autocrlf? or is it unrelated to the problem?):

> 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.

Also now gits insist on changing lineendings in a couple of files (which is what I do not want).

solution

I ended up with this in the attributes file, thanks to :

*.cpp -text crlf diff

the -text part was needed to keep git from converting lineends, even though core.autocrlf is set to false.

标签: git
2条回答
爱情/是我丢掉的垃圾
2楼-- · 2020-03-03 08:16

It is treating some files as binary, because they have wrong file encoding. It should work ok, if you convert those files to UTF-8 (or to the same encoding that in normal files). To change file encoding use notepad++ or any another way.

查看更多
够拽才男人
3楼-- · 2020-03-03 08:38

Try adding the following line to your $repo/.git/info/attributes:

*.cpp crlf diff

You can specify it in gitattributes system or user-wide.

查看更多
登录 后发表回答