Set file as non-binary in git

git
10,441

Solution 1

You can do this to force git to think it's text:

 *.cs diff

You'll want to make sure it actually is text though. Forcing Git to think your file is text when it actually isn't can cause extremely bad behavior in a variety of situations.

You may need to set a couple of other attributes too:

 *.cs diff merge text

The text is useful for EOL normalization. You might need merge if Git still thinks the files are binary at merge time.

However, the real question is "Why is Git marking my file as binary?" The answer is because it's seeing a NUL (0) byte somewhere within the first 8000 characters of the file. Typically, that happens because the file is being saved as something other than UTF-8. So, it's likely being saved as UCS-2, UCS-4, UTF-16, or UTF-32. All of those have embedded NUL characters when using ASCII characters. So, while your question says you did re-saved the files as UTF-8, you may want to check again with a hex editor. I suspect that they are not UTF-8, and that's the core of the problem.

Solution 2

Use Notpad++ to change the encoding from anything other than an encoding with a Byte Order Mark (BOM). Currently, git sees these top BOM characters (\0xFF\0xFE) as the start of a binary file.

Notepad++ Encoding Menu

Share:
10,441

Related videos on Youtube

Creative Magic
Author by

Creative Magic

Front-end developer with 6 year of experience. I specialize in making games with: Flash Action Script 3 development Haxe / OpenFL JavaScript Accepting projects at [email protected] Please send project description, deadline and budget.

Updated on June 04, 2022

Comments

  • Creative Magic
    Creative Magic almost 2 years

    I have a folder with lots of .cs files. Some of these files (for some reason) are tracked as binary and the git diff command doesn't work normally with them. I tried re-saving all these files to UTF-8 encoding, but it didn't help. I tried changing the directory, directory name, filename and file extension and all of these helped.

    I also tried modifying the .gitattributes file to treat *.cs files as non-binary but it didn't help me:

    *.cs diff=csharp
    

    I need a way to set all these files as non-binary w/o changing their path or name. Is there such a way?

  • Creative Magic
    Creative Magic over 10 years
    Checked my file with SublimeText 2, the encoding is shown UTF-8, with (or w/o) the git attributes you've written I get the same response: username$ git diff diff --git a/PATH/FileName.cs b/PATH/FileName.cs index 3936d6d..a6730f9 100644 Binary files a/PATH/FileName.cs and b/PATH/FileName.cs differ username$ and renaming the file helps...
  • CB Bailey
    CB Bailey over 10 years
    If there Git thinks the file is binary you probably don't want to use text, though as it would seem that there are characters not being properly interpreted by Git (stateful encoding??), so forcing Git to do line ending mangling might produce undesirable results.
  • John Szakmeister
    John Szakmeister over 10 years
    @CharlesBailey Right, that's why I said "You'll want to make sure it actually is text though." But, I'll make it more clear that it can cause Bad Thing to Happen, if you force the situation.
  • John Szakmeister
    John Szakmeister over 10 years
    @CreativeMagic then something else is happening. Look in your tree for another .gitattributes file that is setting up your file differently. Where is the .gitattributes file your setting and what for what path does the file that's causing you problems live? Somewhere in between there is likely another .gitattributes file that's causing the issue.
  • John Szakmeister
    John Szakmeister over 10 years
    Ah, then maybe you were using the wrong name in the .git/info/ folder. The name should be .git/info/attributes in the info folder. You still want to make sure that the file is not binary though. A UTF-8 file should not be detected that way. There's likely still something else happening here that's at the root of the issue (e.g., a bad gitattributes rule that is causing the .cs files to be treated as binary).