How to prefer files from one branch during a merge?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
When Git says two branches conflict, there are several different ways to "prefer one branch". The important part is choosing the right mechanism, because -X theirs, --theirs, and -s ours do not mean the same thing and can produce very different results.
Know What "Ours" and "Theirs" Mean
During a normal merge, ours means the branch you currently have checked out, and theirs means the branch you are merging in. If you are on main and run git merge feature, then:
- '
oursismain' - '
theirsisfeature'
That naming trips people up constantly. If you misunderstand it, you can keep the wrong side of the conflict and only notice after the merge is committed.
Prefer the Other Side Only for Conflicting Hunks
If you want Git to favor the incoming branch when a conflict happens, use the recursive merge strategy option:
This does not mean "replace all files with the feature branch version." It only means that when Git encounters conflicting hunks, it chooses the theirs side automatically. Non-conflicting changes from both branches are still merged normally.
That makes -X theirs useful when most conflicts should lean toward the incoming branch, but you still want Git's standard merge behavior everywhere else.
Prefer a Specific File After a Conflict
If only a few files should come from one branch, it is usually better to merge normally and then pick the desired side on a per-file basis.
For the incoming branch version of one file:
For the current branch version:
This is often the safest workflow because you can inspect the conflict first and then decide file by file instead of committing to a repo-wide preference rule.
Do Not Confuse -X theirs With -s ours
This is the most important distinction in the topic.
-X theirs:
- resolves conflicting hunks in favor of the other branch
- still incorporates non-conflicting changes from both branches
-s ours:
- records a merge commit
- ignores the other branch's content entirely
- keeps the current branch tree as the result
Example:
That command says, in effect, "mark feature as merged, but keep my current files exactly as they are." It is a specialized history-management tool, not a conflict-resolution shortcut.
Prefer One Side for a Whole Directory
Sometimes only a directory should come from one branch. In that case, merge first, then restore that path from the desired side.
This pattern is useful when generated files, documentation, or config directories should consistently favor one branch's state.
A Safe Workflow
If the merge matters, the safest approach is:
- start the merge
- inspect the conflicts
- use
--oursor--theirsonly on the files that truly need it - review the staged diff before committing
Useful commands:
That is slower than blind automation, but it drastically reduces accidental data loss.
Common Pitfalls
The biggest mistake is misunderstanding ours and theirs. Those words are relative to the branch currently checked out, not emotionally tied to the branch you "like more".
Another common issue is expecting -X theirs to replace every conflicting file wholesale. It only affects conflicting sections, while ordinary non-conflicting changes from both branches are still merged.
People also misuse -s ours when they really wanted -X ours. The strategy option and the merge strategy are different tools with very different outcomes.
Finally, never skip reviewing the result. A merge command can complete successfully while still giving you the wrong semantic result for your codebase.
Summary
- '
oursis the checked-out branch andtheirsis the branch being merged in.' - Use
git merge -X theirs branchto prefer the incoming side for conflicting hunks only. - Use
git checkout --theirs pathor--ours pathwhen resolving specific files. - Do not confuse
-X theirswith-s ours; they solve different problems. - Review the merged result before committing, even if Git resolved conflicts automatically.

