Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make Git treat a file as binary?

Tags:

git

Having a problem with a medium sized project where visual studio project files keep having issues due to git treating them as text and merging. I'd like to just set the file as binary so that git won't auto merge these files ever.

Is there a way to do this?

like image 716
Charles Randall Avatar asked Jun 22 '12 18:06

Charles Randall


People also ask

How does Git treat binary files?

Git can usually detect binary files automatically. No, Git will attempt to store delta-based changesets if it's less expensive to (not always the case). Submodules are used if you want to reference other Git repositories within your project.

Is Git good for binary files?

Well git is good with binaries. But it won't handle binaries like text files. It's like you want to merge binary files. I mean, a diff on a jpeg will never return you anything.

Why does Github think my file is binary?

As mentioned in "Set file as non-binary in git": "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.

Should I commit binary files Git?

It's important to never commit binary files because once you've commit them they are in the repository history and are very annoying to remove. You can delete the files from the current version of the project - but they'll remain in the repository history, meaning that the overall repository size will still be large.


2 Answers

Yes, using attributes. Put something like this in your .gitattributes file (create it if it doesn't exist):

*.sln binary
*.suo binary
*.vcxproj binary

Here binary is actually a predefined macro, equivalent to -diff -merge -text.

If you want to still be able to see the diff, you can use:

*.sln -merge -text

This way, the *.sln files won't be merged, not have eol normalized, but meanwhile diff-able.

like image 112
Michael Wild Avatar answered Oct 20 '22 00:10

Michael Wild


You should define binary file attributes in your .gitattributes file (create it if it doesn't exist) by putting these lines in it, to prevent it to handle it as text diff file:

# Define binary file attributes.
# - Do not treat them as text.
# - Include binary diff in patches instead of "binary files differ."
*.sln     -text diff
*.suo     -text diff
*.vcxproj -text diff
*.gif     -text diff
*.gz      -text diff
*.ico     -text diff
*.jpeg    -text diff   
like image 38
Omar Alahmed Avatar answered Oct 19 '22 23:10

Omar Alahmed