Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set file as non-binary in git

Tags:

git

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?

like image 793
Creative Magic Avatar asked Nov 01 '13 08:11

Creative Magic


People also ask

Can git diff binary files?

Diffing binary filesYou can do this by creating a . gitattributes file in the root of your repository. Once configured, git diff will first run the binary file through the configured converter script and diff the converter output.

Should I commit binary files Git?

It's better to uncompress the files and commit the diffable sources, letting Git handle compressing the data in your repo. Avoid committing compiled code and other binary dependencies. Commit the source and build the dependencies, or use a package management solution to version and supply these files to your system.

What is the Gitattributes file?

A gitattributes file is a simple text file that gives attributes to pathnames. Each line in gitattributes file is of form: pattern attr1 attr2 ... That is, a pattern followed by an attributes list, separated by whitespaces. Leading and trailing whitespaces are ignored.

Where do I put the Gitattributes file?

To define these attributes, create a file called . gitattributes in the root directory of your repository and push it to the default branch of your project.


1 Answers

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.

like image 163
John Szakmeister Avatar answered Oct 25 '22 19:10

John Szakmeister