Welcome to the CSC Q&A, where you can get help (and share your knowledge) about computer science!

IMPORTANT: How do I merge binary files in git?

+11 votes
I know that git isn't the greatest about dealing with binary files (but neither are most other version control systems), and it really shouldn't be used for LARGE binary files that change frequently.  So we shouldn't store those in the repository.

However, what if we have a small binary file (like a .db file for SQLite, or some image artwork) that is most convenient to keep track of in your project repository.  It might also be the case that it changes frequently, because every time you run your program, it might update the database in some way.

How should I handle this?  What are some useful tips?
asked Sep 22, 2015 in Fall 15-16 by Forrest Stonedahl (100 points)
Note, this will not be tested on Midterm 1... but it will likely be useful for working with git your projects!

1 Answer

+2 votes

Here are my current thoughts, although depending on your situation there may be a better solution for you.

a) If both you and someone else on your team make changes to the binary file, there is no perfect way to merge your changes.  You'll probably just need to choose one person's file or the other to go forward with.

b) Probably try NOT to commit these files frequently, unless you really are making a change to the binary file that you want to be shared with others in your group.

c) If you have *unstaged/uncommitted* changes to the file, and someone else has changed it up on GitHub, and you don't care about your changes... it might make sense to do "git checkout -- myfile.db" to reset your working directory file to match the version in your local repository, before pulling, and then you'll avoid conflicts, and just get your teammates changes to the file (and your own will be discarded).

d) If you have committed your changes to the binary file, and someone else has committed and pushed their changes, then when you pull you'll get a nasty conflict.  Eclipse will show it as a RED diamond on the binary file, and if you double-click on it, it may open the file in a text editor, but there will be binary garbage, and editing a binary file with a text editor is most likely to corrupt it. So give up on that!  Instead:

As usual, when things get tricky with git, you need to go to the Git Bash command line.  cd to the folder with the file in question, and run either:

 1) If you want to keep YOUR version of the file, and discard your teammates version:

git checkout --ours -- myfile.db

git add myfile.db      <-- as usual, to mark the conflict as resolved

 2) If you want to keep THEIR version of the file, and discard your own version:

git checkout --theirs -- myfile.db

git add myfile.db      <-- as usual, to mark the conflict as resolved


Finally, if other people have good strategies/recommendations for working with binary files in git, resolving conflicts, or avoiding conflicts in the first place, please add your own answers here!

answered Sep 22, 2015 by Forrest Stonedahl (100 points)
Note: (Even if you don't use binary files, you can use these same commands for dealing for resolving conflicts for non-binary files, if you want to just keep one version or the other, and not manually merge them.)