Advanced Merge Strategies
You'll learn how to apply specialized merge strategies—ours, theirs, and subtree—to handle complex integration scenarios beyond standard three-way merges.
Why Custom Strategies Matter
Sometimes a regular merge isn't appropriate. Imagine you're merging a documentation branch where you know your version is always correct, or integrating an external library as a subdirectory. Git's advanced merge strategies let you automate these decisions.
The Three Key Strategies
Ours Strategy
When conflicts arise, always keep your current branch's version. Think of it as saying "I trust my side completely."
git merge -s ours feature-branch
This creates a merge commit but discards all changes from feature-branch. Useful when you want to mark branches as merged without actually incorporating their content.
Theirs Strategy (via Recursive)
Git doesn't have a pure -s theirs, but you can prefer the incoming branch's changes during conflicts:
git merge -X theirs feature-branch
The -X theirs option tells the recursive strategy to favor the other branch when conflicts occur. Perfect when integrating upstream changes you trust.
Subtree Merge
Merges one repository into a subdirectory of another without submodules. Like adopting an entire external project as a folder:
git merge -s subtree external-library
Git intelligently matches directory structures, ideal for vendoring dependencies or consolidating related projects.
When to Use Each
- Ours: Recording branch history without code changes; resolving "already fixed" conflicts
- Theirs: Accepting upstream refactors; automated integration pipelines
- Subtree: Managing embedded projects; migrating from submodules
Key Takeaway: Advanced merge strategies—ours, theirs (via -X), and subtree—give you surgical control over how Git integrates conflicting histories, automating decisions that would otherwise require manual conflict resolution.