Merge Strategies
A merge strategy is the method Git uses to combine branches in git merge (and in git rebase, git cherry-pick, git revert and git pull). You choose one with -s <strategy>. Almost always, the default strategy, ort, is right. A strategy option, given with -X <option>, changes how a strategy behaves, for example by resolving conflicts in favor of one side or by ignoring changes in spaces. They are used for the special cases: merging many branches at once, recording a merge without taking any of its changes, or settling every conflict the same way.
Think of two editors combining their corrections to a book. The strategy is their method: compare page by page, take only one editor's copy, or combine five editors at once. Strategy options are house rules, such as "when we disagree, my corrections win".
Syntax
git merge -s <strategy> <branch>...
git merge -X <option> <branch>| Strategy | Description |
|---|---|
ort | It is used for every normal merge of two branches. The default since Git 2.34. |
recursive | It is the older default, kept for compatibility. There is no reason to choose it today. |
octopus | It is used to merge more than two branches in one merge commit. The default when you name several branches. |
ours | It is used to record a merge while keeping only the current branch's content, ignoring the other branch completely. |
subtree | It is used to merge a project that lives in a subfolder (see "Git Subtree"). |
resolve | It is an old, simple strategy for two branches. Rarely used. |
Strategy option (-X) | Description |
|---|---|
-X ours | It is used to settle each conflicting part in favor of the current branch, while still taking the other branch's non-conflicting changes. |
-X theirs | It is used to settle each conflicting part in favor of the other branch. |
-X ignore-space-change | It is used to treat changes in the amount of spaces as no change. ignore-all-space ignores spaces completely. |
-X diff-algorithm=<name> | It is used to choose the diff algorithm, such as histogram or patience (see "How Git Merges"). |
-X renormalize | It is used to apply line-ending rules before comparing (see "Git .gitattributes and Line Endings"). |
-X find-renames=<n> | It is used to set how similar two files must be to count as a rename, such as 70%. |
The Starting Point
Navika and Advitha both changed the late fee, and Advitha also changed the hostel fee:
mkdir fee-tracker
cd fee-tracker
git init --quiet
printf "%s\n" 'Term fee: $12,000' 'Late fee: $250' 'Exam fee: $800' 'Hostel fee: $9,000' > fees.txt
git add fees.txt
git commit --quiet -m "Add the fee list"
git switch --quiet -c advitha/fees
printf "%s\n" 'Term fee: $12,000' 'Late fee: $275' 'Exam fee: $800' 'Hostel fee: $9,500' > fees.txt
git commit --quiet -am "Update the late and hostel fees" --author="Advitha Anamaneni <advitha@example.com>"
git switch --quiet main
printf "%s\n" 'Term fee: $12,000' 'Late fee: $300' 'Exam fee: $800' 'Hostel fee: $9,000' > fees.txt
git commit --quiet -am "Raise the late fee"
git log --oneline --graph --all* cad3eba (HEAD -> main) Raise the late fee
| * 0d403fd (advitha/fees) Update the late and hostel fees
|/
* 885c303 Add the fee listExample 1: The Default, ort
git merge advitha/feesAuto-merging fees.txt
CONFLICT (content): Merge conflict in fees.txt
Automatic merge failed; fix conflicts and then commit the result.Explanation: ort ("Ostensibly Recursive's Twin") compared both branches with their common starting point. The late fee line changed on both sides, so it conflicts. The hostel fee changed only on Advitha's side, and the term and exam fees on neither, so none of those conflict. (The unchanged exam fee line between them matters: changes on directly neighboring lines would count as one conflict.) Navika cancels to try the alternatives:
git merge --abort Example 2: -X ours and -X theirs
git merge --quiet -X ours advitha/fees -m "Merge Advitha's fees, keeping our late fee"
cat fees.txtAuto-merging fees.txt
Term fee: $12,000
Late fee: $300
Exam fee: $800
Hostel fee: $9,500Explanation: with -X ours, the conflicting late fee was settled in favor of the current branch ($300), but Advitha's hostel fee change, which didn't conflict, was taken ($9,500). No conflict markers, no stop. -X theirs does the opposite:
git reset --quiet --hard ORIG_HEAD
git merge --quiet -X theirs advitha/fees -m "Merge Advitha's fees, keeping her late fee"
cat fees.txtAuto-merging fees.txt
Term fee: $12,000
Late fee: $275
Exam fee: $800
Hostel fee: $9,500Explanation: git reset --hard ORIG_HEAD undid the previous merge (see "Undoing a Merge"). With -X theirs, the late fee came from Advitha ($275), and so did the hostel fee, as before.
Example 3: The ours Strategy
-s ours is a different thing with a confusingly similar name. It records a merge commit but keeps the current branch's content exactly as it is, taking nothing from the other branch:
git reset --quiet --hard ORIG_HEAD
git merge --quiet -s ours advitha/fees -m "Record Advitha's branch as merged, keep main as it is"
cat fees.txt
git log --oneline --graphTerm fee: $12,000
Late fee: $300
Exam fee: $800
Hostel fee: $9,000
* 68ea8ec (HEAD -> main) Record Advitha's branch as merged, keep main as it is
|\
| * 0d403fd (advitha/fees) Update the late and hostel fees
* | cad3eba Raise the late fee
|/
* 885c303 Add the fee listExplanation: the hostel fee stayed at $9,000: none of Advitha's changes came in. But the history shows a merge, so Git now treats advitha/fees as merged. This is used to "close" a branch whose work was replaced or redone in another way, so that it no longer shows up as unmerged and later merges don't bring its changes back.
-X ours (option of ort) | -s ours (strategy) | |
|---|---|---|
| Non-conflicting changes from the other branch | Taken | Ignored |
| Conflicting parts | Ours | Ours (everything is ours) |
| Typical use | A merge where our version should win each disagreement | Marking a branch as merged without its changes |
Is there a theirs version? | Yes, -X theirs | No. Git has no -s theirs |
Example 4: Octopus, Many Branches at Once
Three small features are ready, each in its own file, and Navika has meanwhile made one more commit on main:
for item in exam bus books; do
git switch --quiet -c feature/$item main
echo "${item} fee" > $item.txt
git add $item.txt
git commit --quiet -m "Add the $item fee"
done
git switch --quiet main
echo "Books are billed every term" >> fees.txt
git commit --quiet -am "Note the books billing"
git merge feature/exam feature/bus feature/books -m "Merge the exam, bus and books fees"
git log --oneline --graph -5Trying simple merge with feature/exam
Trying simple merge with feature/bus
Trying simple merge with feature/books
Merge made by the 'octopus' strategy.
books.txt | 1 +
bus.txt | 1 +
exam.txt | 1 +
3 files changed, 3 insertions(+)
create mode 100644 books.txt
create mode 100644 bus.txt
create mode 100644 exam.txt
*---. e868d4b (HEAD -> main) Merge the exam, bus and books fees
|\ \ \
| | | * 7332166 (feature/books) Add the books fee
| | * | 223da26 (feature/bus) Add the bus fee
| | |/
| * / 712d232 (feature/exam) Add the exam fee
| |/
* / 5304c84 Note the books billing
|/Explanation: naming several branches switches to the octopus strategy, which merged them one after another and made one merge commit with four parents. Octopus refuses to continue when there is a conflict that needs manual work: then merge the branches one at a time. It is popular in projects that merge many independent topic branches together, and can make the graph harder to read.
Example 5: Ignoring Changes in Spaces
Advitha's editor re-indented a Java file, while Navika changed one of the same lines:
printf "%s\n" "class Fees {" " int late = 250;" " int hostel = 9000;" "}" > Fees.java
git add Fees.java
git commit --quiet -m "Add Fees.java"
git switch --quiet -c advitha/indent
printf "%s\n" "class Fees {" " int late = 250;" " int hostel = 9000;" "}" > Fees.java
git commit --quiet -am "Use four spaces" --author="Advitha Anamaneni <advitha@example.com>"
git switch --quiet main
printf "%s\n" "class Fees {" " int late = 300;" " int hostel = 9000;" "}" > Fees.java
git commit --quiet -am "Raise the late fee in code"
git merge --quiet -X ignore-space-change advitha/indent -m "Merge the indentation change"
cat Fees.javaAuto-merging Fees.java
class Fees {
int late = 300;
int hostel = 9000;
}Explanation: without the option, the late line would conflict, because both sides changed it. With -X ignore-space-change, Git treats a line that only changed in spaces as unchanged. So Advitha's re-indentation counted as no change at all, and Navika's real change to late merged without a conflict. Notice what that means for the result: every line kept Navika's two spaces, so the re-indentation itself is gone. That is the usual trade-off: merge the real changes with this option, then run the formatter again and commit the new indentation. The option ignore-all-space goes further and ignores spaces even where there were none before.
Choosing a Strategy
| Situation | Use |
|---|---|
| Any normal merge, rebase, pull or cherry-pick | The default, ort |
| Merging several independent branches into one commit | octopus (automatic when naming several branches) |
| Recording a branch as merged without its changes | -s ours |
| You know one side should win every conflict | -X ours or -X theirs |
| A branch only re-indented code | -X ignore-space-change |
| A project inside a subfolder | -s subtree, or git subtree |
Common Mistakes
Confusing -s ours with -X ours. One drops all of the other branch's changes, the other only wins conflicts.
Looking for -s theirs. It doesn't exist. To replace a branch's content with another's, use -X theirs, or git merge -s ours from the other side.
Using -X theirs to avoid reading conflicts. It may silently discard important changes on your side.
Expecting -X ours and -X theirs to mean the same in a rebase. During a rebase, "ours" is the branch you are rebasing onto and "theirs" is your own commits being replayed (see "Git Rebase").
Octopus merges with conflicts. Octopus stops. Merge branches one at a time instead.
Under the Hood
Every strategy is selected by name, and for most of them Git runs a program called git-merge-<strategy> from its exec-path (git --exec-path shows where); ort is built into Git itself. Strategy options are passed through to it. ort replaced recursive as the default in Git 2.34. It does the same kind of three-way merge, but much faster, especially with many renames, and it builds the result in memory before touching your working folder, so a failed merge never leaves the working folder half-updated. When the branches have more than one best common ancestor, both ort and recursive first merge those ancestors into a virtual merge base, which is where the old name "recursive" comes from (see "How Git Merges"). octopus merges the branches into the result one by one, and ours simply writes a merge commit whose tree is the current one.
Interview Corner
What is the default merge strategy in Git?
ort, since Git 2.34. It replaced recursive, doing the same three-way merge faster and with better rename handling.
What is the difference between git merge -s ours and git merge -X ours?
-s ours is a strategy that records a merge but keeps the current branch's content entirely, ignoring the other branch. -X ours is an option of ort that settles only conflicting parts in favor of the current branch, while still taking the other branch's non-conflicting changes.
What is an octopus merge?
A merge of more than two branches into one commit with several parents. Git uses the octopus strategy when you name several branches, and it stops if a conflict needs manual work.
How do you merge a branch that only changed indentation without conflicts?
git merge -X ignore-space-change <branch> (or ignore-all-space), so changes in spaces count as no change.
Practice
Challenge: theirs for one merge. Advitha's branch is right about every conflict. Create a branch advitha/late from the commit before Navika's "Raise the late fee in code", change the late fee in Fees.java to 275 (with two spaces), and merge it into main so that her side wins the conflict. Print the late line.
What does Git print? Before you open the solution, decide which value and how many spaces the line has.
Show solution
git switch --quiet -c advitha/late "main^{/Add Fees.java}"
printf "%s\n" "class Fees {" " int late = 275;" " int hostel = 9000;" "}" > Fees.java
git commit --quiet -am "Set the late fee to 275 in code" --author="Advitha Anamaneni <advitha@example.com>"
git switch --quiet main
git merge --quiet -X theirs advitha/late -m "Merge Advitha's late fee"
grep late Fees.javaAuto-merging Fees.java
int late = 275;main^{/Add Fees.java} finds the newest commit reachable from main whose message matches "Add Fees.java" (see "Naming Commits"). With -X theirs, the conflicting late line came from Advitha's branch, with her value, 275. Every other line was the same on both sides.
Checkpoint. In a practice repository, create the same conflict twice and merge it once with -X ours and once with -s ours. Compare the results with git diff and explain the difference in your own words.
Key Takeaways
Quick reference
| Command | It is used to |
|---|---|
git merge <branch> | Merge with ort |
git merge -X ours <branch> / -X theirs | Settle conflicts for one side |
git merge -s ours <branch> | Record a merge, keep our content |
git merge <b1> <b2> <b3> | Octopus merge |
git merge -X ignore-space-change <branch> | Ignore changes in spaces |
Was this tutorial helpful?