Why does Git treat this text file as a binary file?

Viewed 128034

I wonder why git tells me this?

$ git diff MyFile.txt
diff --git a/MyFile.txt b/MyFile.txt
index d41a4f3..15dcfa2 100644
Binary files a/MyFile.txt and b/MyFile.txt differ

Aren't they text files?

I have checked the .gitattributes and it is empty. Why I am getting this message ?, I cannot get diffs as I use to anymore

ADDED :

I've noticed there is an @ in the file permissions, what is this ?, Could this be the reason ?

$ls -all
drwxr-xr-x   5 nacho4d  staff    170 28 Jul 17:07 .
drwxr-xr-x  16 nacho4d  staff    544 28 Jul 16:39 ..
-rw-r--r--@  1 nacho4d  staff   6148 28 Jul 16:15 .DS_Store
-rw-r--r--@  1 nacho4d  staff    746 28 Jul 17:07 MyFile.txt
-rw-r--r--   1 nacho4d  staff  22538  5 Apr 16:18 OtherFile.txt
16 Answers

This is also caused (on Windows at least) by text files that have UTF-8 with BOM encoding. Changing the encoding to regular UTF-8 immediately made Git see the file as type=text

I had an instance where .gitignore contained a double \r (carriage return) sequence by purpose.

That file was identified as binary by git. Adding a .gitattributes file helped.

# .gitattributes file
.gitignore diff

If git check-attr --all -- src/my_file.txt indicates that your file is flagged as binary, and you haven't set it as binary in .gitattributes, check for it in /.git/info/attributes.

I had a similar issue as I pasted some text from a binary Kafka message, which inserted non-visible character and caused git to think the file is binary.

I found the offending characters by searching the file using regex [^ -~\n\r\t]+.

  • [ match characters in this set
  • ^ match characters not in this set
  • -~ matches all characters from ' ' (space) to '~'
  • \n newline
  • \r carriage return
  • \t tab
  • ] close set
  • + match one or more of these characters

Change the Aux.js to another name, like Sig.js.

The source tree still shows it as a binary file, but you can stage(add) it and commit.

The reason my file was showing as binary (an dI was getting no diff using git diff or SourceTree) was because the file in question was added as a Git LFS file

Git (and SourceTree) do not seem to be able to diff text files added to LFS. However after a bit of hunting and I was able to fix this by running... git config --global diff.lfs.textconv cat

with help from the suggestion here... https://github.com/git-lfs/git-lfs/issues/440#issuecomment-501007460

I got the same message when the files I was diff-ing were generated in the Powershell terminal using the echo command:

echo "new file" > newfile.txt

The files remained binary even after I have opened and edited them with an editor.

The quick and dirty solution for me was to copy the content of those files, delete them, create them again directly from the editor (not from the terminal), and paste back the contents. Diff-ing afterwards showed the correct per-line conflicts as one would expect.

Related