151

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?

Charles Randall
  • 6,920
  • 12
  • 33
  • 38
  • 2
    But Visual Studio project files are text files and do need to be merged? – CB Bailey Jun 22 '12 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. – michael.bartnett Apr 19 '13 at 16:26
  • 1
    @michael.bartnett: Only if you mess up the merge, surely? – CB Bailey Apr 19 '13 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](http://bitsquid.se/presentations/collaboration.pdf) for more on the notion. – michael.bartnett Apr 19 '13 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 '13 at 16:39
  • @CharlesBailey Yes. I could also tell git to treat some text file as binary until I have time to make that merge driver. Your initial comment seemed to me to imply that this isn't a worthwhile step, and although that may be the case for visual studio project files, that doesn't necessarily hold for other types of files. I wanted my disagreement on the record, so I replied. – michael.bartnett Apr 19 '13 at 17:02
  • @michael.bartnett: In my opinion, it isn't a worthwhile step, it's a retrograde step. Instead of getting some help with you merge, you get no help. Minor changes to different parts of a project file will often merge cleanly in my experience and, provided you check the results of the merge before blindly pushing, even quite complex project merges are easier done by fixing up a partially complete automerge by hand than manually applying one set of project changes to a different version of the project file one at a time through the IDE. IMHO. – CB Bailey Apr 19 '13 at 19:35
  • The contrary: force it to treat a file as text: http://stackoverflow.com/questions/777949/can-i-make-git-recognize-a-utf-16-file-as-text?lq=1 – Ciro Santilli OurBigBook.com Jun 21 '14 at 09:37

2 Answers2

182

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.

ryenus
  • 15,711
  • 5
  • 56
  • 63
Michael Wild
  • 24,977
  • 3
  • 43
  • 43
  • 2
    where is this file? – neves Jun 07 '16 at 22:45
  • 1
    @neves Everywhere a gitignore file can be located, as per the documentation provided by the link in the given answer. – Michael Wild Jun 09 '16 at 04: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 http://stackoverflow.com/questions/28026767/where-should-i-place-my-global-gitattributes-file – jan-glx Oct 09 '16 at 15:01
  • What happens to these files if they are not automatically merged? Is one version picked or do you have to merge manually? – ClydeTheGhost Jun 28 '19 at 19:22
  • 1
    Another useful thing is `linguist-generated=true` for auto-generated files like db or graphql schema dump. – elquimista Jun 30 '19 at 11:03
  • 1
    @ClydeTheGhost you'll have to pick one. – Michael Wild Jul 01 '19 at 19:30
14

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   
Omar Alahmed
  • 1,082
  • 12
  • 13