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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With