153

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?

8
  • 2
    But Visual Studio project files are text files and do need to be merged?
    – CB Bailey
    Jun 22, 2012 at 20:20
  • 2
    @CharlesBailey They will periodically merge in the worst way possible, screwing up all your builds. I don't want to fiddle with the XML, I want it to ask me to manually add new files or possibly even run an XML-diffing tool that knows how to handle that properly. Apr 19, 2013 at 16:26
  • 1
    @michael.bartnett: Only if you mess up the merge, surely?
    – CB Bailey
    Apr 19, 2013 at 16:28
  • 1
    @CharlesBailey Of course if I'm manually merging it's not a problem. But that's not something I should have to manually merge. It's usually a build item add/removal/edit...which is totally auto-mergeable, but not by the usual diff per line method. Check out this presentation for more on the notion. Apr 19, 2013 at 16:36
  • @michael.bartnett: Setup a better merge driver, then. The point I was making (10 months ago) was that project files can and should be merged. Personally, I much prefer to fix up a reasonable machine attempt than to do it totally by hand, that seems like unnecessary work.
    – CB Bailey
    Apr 19, 2013 at 16:39

2 Answers 2

187

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.

6
  • 2
    where is this file?
    – neves
    Jun 7, 2016 at 22:45
  • 1
    @neves Everywhere a gitignore file can be located, as per the documentation provided by the link in the given answer. Jun 9, 2016 at 4:04
  • 6
    @neves : you got to make one, either locally in your repo or globally as e.g. ~/.gitattributes and then running git config --global core.attributesfile ~/.gitattributes see stackoverflow.com/questions/28026767/…
    – jan-glx
    Oct 9, 2016 at 15:01
  • 1
    Another useful thing is linguist-generated=true for auto-generated files like db or graphql schema dump.
    – elquimista
    Jun 30, 2019 at 11:03
  • 1
    @ClydeTheGhost you'll have to pick one. Jul 1, 2019 at 19:30
15

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   

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.