Merge Strategies
You'll learn how Git intelligently chooses different algorithms to merge branches based on the complexity of your code history.
What Are Merge Strategies?
When you merge branches in Git, the system doesn't just smash code together blindly—it uses specific merge strategies (algorithms) to decide how to combine changes. Think of these strategies as different tools in a toolbox: you'd use a screwdriver for screws and a hammer for nails. Similarly, Git selects or lets you specify which strategy fits your merge scenario.
Common Merge Strategies
Recursive (Default)
The recursive strategy is Git's workhorse for three-way merges. When two branches have diverged and both have new commits, Git finds their common ancestor and intelligently combines changes. It can even handle complex scenarios like multiple common ancestors by recursively merging those ancestors first.
Ours
The ours strategy resolves conflicts by keeping your current branch's version of conflicting files, completely ignoring changes from the branch being merged in. This is useful when you want to record a merge in history but preserve your branch's code entirely—perhaps to supersede an experimental branch without actually adopting its changes.
Theirs
The theirs strategy (used via options like git merge -X theirs) prefers the incoming branch's changes during conflicts. It's handy when you trust the other branch's version more than your own for disputed sections.
You can specify strategies explicitly:
git merge -s ours feature-branch
Key Takeaway: Git's merge strategies provide algorithmic flexibility—recursive handles complex histories intelligently, ours keeps your changes, and theirs favors incoming changes, letting you control exactly how branches combine.