Skip to content
Tutorials Center
My learning

Topics

Git Reference ยท Merging and Rebasing

Git merge strategies

Reference for git merge strategies and strategy options: ort, recursive, resolve, octopus, ours and subtree, plus -X ours, -X theirs and whitespace options.

A merge strategy is the algorithm git merge uses to combine two or more histories. You pick one with -s <strategy>. Most of the time you never do: Git chooses ort for two branches and octopus for more. The other strategies exist for special jobs, such as recording a merge without taking any changes, or merging a project that lives in a subdirectory.

Do not confuse a strategy (-s) with a strategy option (-X). A strategy decides how the whole merge is done. An option tunes the default behavior, for example "when both sides changed the same lines, prefer ours". The names ours and theirs appear in both places and mean very different things, as the first example shows.

Terminal
git merge -s ours old-idea            # keep my tree, record the merge
git merge -X theirs feature           # normal merge, theirs wins conflicts
git merge a b c                       # octopus (chosen automatically)

Try it

Bash Strategy ours versus option ours and theirs

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Octopus and ours as a bookkeeping merge

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
git merge -s <strategy> [-X <option>]... <commit>...
Strategy What it does
ort The default for one other branch. A three-way merge that detects renames, handles criss-cross histories by merging the common ancestors first, and is much faster than recursive.
recursive The former default, now an alias of ort (options such as patience are accepted).
resolve A simple three-way merge with one base. Handles criss-cross merges less carefully and finds fewer renames. Rarely better than ort.
octopus The default when merging more than two heads. Merges many branches into one commit but refuses any merge that needs manual conflict resolution.
ours Records a merge but keeps the current branch's tree exactly. Everything from the other branch is thrown away.
subtree Like ort, but first shifts one tree to match the other's path prefix. For merging a project that lives in a subdirectory.
Option (-X) Applies to Description
ours ort, recursive Resolve conflicting hunks in favor of the current branch. Non-conflicting changes from both sides are still merged.
theirs ort, recursive Resolve conflicting hunks in favor of the merged branch.
ignore-space-change, ignore-all-space, ignore-space-at-eol, ignore-cr-at-eol ort, recursive Treat whitespace differences as no change.
renormalize ort, recursive Run a virtual check-out and check-in of all three stages, which helps when line-ending or clean-filter settings changed.
find-renames[=<n>], no-renames ort, recursive Set or disable rename detection and the similarity threshold.
diff-algorithm=<name> (also patience, histogram, minimal) ort, recursive Choose the diff algorithm used for the content merge.
subtree[=<path>] ort, recursive Shift trees by a path prefix (guessed if omitted).

Errors: an unknown strategy fails before any change. octopus stops with merge program failed when files conflict.

Usage notes

  • -s ours is not -X ours. The strategy discards the other side completely. The option keeps a real merge and only decides conflicts. Mixing them up is a classic way to lose a week of work.
  • Use -s ours to retire a branch. It marks the other history as merged, so later merges of that branch bring nothing in. Be sure that is what you want; the changes are not in the result.
  • -X theirs can hide a mistake. It silently picks one side for every conflicting hunk, so review the diff afterwards with git diff HEAD^ HEAD or -m --first-parent.
  • ort (Git 2.33+) replaced recursive as the default. It is faster, handles directory renames, and never touches the working tree until the merge is computed. Old scripts that pass -s recursive still work.
  • subtree versus -X subtree. The strategy is the older form; with -X subtree=<path> you name the prefix explicitly. See git subtree for the friendlier front end.
  • Octopus merges are good for combining many independent topic branches (the Git project does this for releases). Resolve conflicts with ordinary merges first.
  • Custom drivers. Strategies work on the whole tree; per-file behavior (for example "always keep my lock file") comes from .gitattributes merge drivers. See Merge strategies and drivers.
  • Rebase and cherry-pick use the same engine: git rebase -X theirs flips the meaning of ours and theirs, because the branch being replayed is "theirs" while the new base is "ours".