How do I make Git treat a file as binary?

Git

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

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.

Solution 2 - Git

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   

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCharles RandallView Question on Stackoverflow
Solution 1 - GitMichael WildView Answer on Stackoverflow
Solution 2 - GitOmar AlahmedView Answer on Stackoverflow