TIL: “ours" and "theirs" git merge strategies
When I’m either:
- working on an experimental branch that I have no intention of merging into
main, yet want to incorporate updates into; or - if I know with 100% certainty that I want to resolve merge conflicts in a particular direction (my branch vs. the one being merged in)
going through every individual merge conflict by hand is annoying.
Recently, I learned a cool trick that that allows me to resolve in one direction or the other: git merge -X ours and git merge -X theirs.
What are these and what do they do?
ours and theirs are git merge strategies.
ours resolves in favour of the current set of changes. For example, your feature branch.
theirs resolves in favour of the branch being merged in, such as main.
This can be done for all merge conflicts within a branch, and also more selectively with using git checkout:
git checkout --ours foo.py git checkout --theirs bar.py
I think there’s other commands that allow the use of ours/theirs (like rebase) but I’ve yet to explore and try them out.
Erica Pisani Newsletter
Join the newsletter to receive the latest updates in your inbox.