I am dealing with quite a few binary files generated by a program. Auto merging doesn't work well for these files. What I'd like to do when merging, is quite literally take one copy or the other. It's not ideal, but I think it's the best approach.
Now my understanding, is that if I wanted to take a file from the branch I'm merging from, and completely discard our version, I should do:
git merge <branchname> git checkout --theirs <filename> git add <filename> git commit
Is this correct, or am I missing something?
I'd like to do it without using .gitignore
Merging is Git's way of putting a forked history back together again. The git merge command lets you take the independent lines of development created by git branch and integrate them into a single branch.
So merging one branch into another has a secondary effect: it moves the merge base of those two branches. In particular, the new merge base of the two branches is now the second parent of the merge commit.
A three-way merge involves three snapshots. Two are the ones that are involved in a two-way merge, and the third one is the base file or the common ancestor with which these two files will be compared. As you can see, C3 is the common ancestor with which C4 and F3 will be compared for merging.
If your goal is indeed to keep one version or the other, then yes, you will want to use:
git checkout <--theirs|--ours> <path> git add <path>
Of course, as you say, it's not ideal. If there's any way you can avoid this, you should. If you can, try to adopt workflow habits which avoid changing those files on divergent branches which will later need to be merged. If the files are generated from tracked content, you really probably do want to ignore them - if you have good reason not to, you might want to generate them from the mergeable tracked content instead, if that's possible.
So, explore all your other options before doing this, but if you must, you've got it right.
Look into using .gitignore to ignore generated files. You may have to do git rm as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With