3

Hy,

I have a problem with a a file in a git repo which is detect as binary and not as text (checked by git dif --stat). The project was converted from svn some years ago, so i could not really change the past. I've now tried to convert the file to a utf8(without bom) file, but git tells me (git diff --stat) that it is binary for git.

Is there any possibilty to change the base type of a file (text|binary) after a commit?

Michael Aigner
  • 4,820
  • 4
  • 21
  • 33
  • 1
    Git doesn't store a "type" for a file, it examines it's contents and determines if it's binary by that. Related: https://stackoverflow.com/q/6855712/1615903 – 1615903 Jun 30 '17 at 06:28
  • 1
    Possible duplicate of [Why does Git think my cs file is binary?](https://stackoverflow.com/questions/2506041/why-does-git-think-my-cs-file-is-binary) – 1615903 Jun 30 '17 at 06:30
  • 1
    hey. the link doesn't help me. My file has only ascii charaters, but git diff --stat displays bin. Every other file is detect as non binary. So what can i change to get that effect. I have already create a new ascii file and write one charater in it, but it also say binary – Michael Aigner Jun 30 '17 at 06:59
  • There are several reasons why git thinks your file is binary. Does your file contain any 0-byte near the start (I don't know how far into the file git searches). Do you have a .gitattributes file that tells git that the file extension means binary? – Lasse V. Karlsen Jun 30 '17 at 07:47
  • no, i replaced the whole file with just the content 'a', so it's only one character – Michael Aigner Jun 30 '17 at 08:22
  • 1
    So gitattributes then, do you have one? What is the extension of the file? And how big is the file now? – Lasse V. Karlsen Jun 30 '17 at 08:50
  • No, i have no gitattributes, because i dont need it for any other file. My file has the .cpp (as 70% of the repo) Extension, a normal c++ file. It has about 1000 lines (i'm not at my desktop, so i can't tell you the bytes) – Michael Aigner Jun 30 '17 at 14:38
  • 3
    *i replaced the whole file with just the content 'a'* If you are running `git diff` Git will check both that content and the previous content; if the *previous* content "appears binary" `git diff` will still say "binary". Only when both inputs appear to be text, or are forced to be considered text, will Git try to diff them as text. (My first guess is that you're on Windows and that particular file is stored as UTF-16, so that most characters are byte-pairs with the upper byte all-zero-bits, which Git sees as binary.) – torek Jun 30 '17 at 14:39
  • Hey torek. Is the only way to change that deleting the actual file, commit it and add the file again and commit again? And yes, I'm on Windows – Michael Aigner Jul 13 '17 at 15:36

1 Answers1

1

The comments basically explain the situation.

Git diff will list binary if the previous commit was detected as binary.

(In my case .gitignore is binary because it has some english and some chinese in it from when I first created it inside my windows cmd/powershell prompt and using ECHO .vs >> .gitignore then adding further to .gitignore using visual studio context menus)

As a result if you really want your next diff to say text for that file you can solve it with two commits.

  1. The first with the correct encoding and a simple text phrase.
  2. The second commit with the correct content and correct encoding.

Now when git diffs the last change it will be comparing text with text and forever more when diffing that file.

Tyeth
  • 699
  • 5
  • 14