W3docs

merge strategies

On this page, you can find useful and detailed information about the merge strategies, explicit and implicit types of them, and subset of operation options.

mergeconflicts

Git Merge Strategies

When the work is complete and ready to be merged into the main line of development, we should choose a merge strategy.

Git combines changes from different branches using different methods, known as “merge strategies”. After selecting a merge strategy, Git creates a new merge commit combining the changes of the specified branches. If not specified, the git merge command will automatically select a merge strategy based on the provided branches. The -s option can be used to specify the strategy name. Here’s the list of merge strategies:

Recursive

git merge recursive strategy

git merge -s recursive branch_name

Git selects recursive as the default strategy when pulling or merging branches. The recursive strategy can detect and manage merges that involve renames, but it cannot use detected copies.

Resolve

merge resolve strategy

git merge -s resolve branch_name

The resolve strategy uses a 3-way merge for resolving branches and can resolve only two HEADs using a 3-way merge algorithm. It is safe and fast and detects criss-cross merge ambiguities in detail.

Octopus

merge octopus strategy

git merge -s octopus branch1 branch2 branch3

When multiple branches are passed to the merge command, the octopus strategy can be used to combine them into a single commit. Octopus refuses if the merge has conflicts that need manual resolution. The basic use of Octopus is to bundle feature branch HEADs that have similarities.

Ours

merge ours strategy

git merge -s ours branch_name

The ours strategy resolves multiple branches, but the result is always that of the current branch HEAD. It ignores all changes from all other branches very effectively. It is intended to be used to replace an old history of side branches.

Subtree

merge subtree strategy

git merge -s subtree branch_name

The subtree strategy is the modified version of the recursive strategy. For example, we merge A and B trees. When corresponding to a subtree of A, B is first modified to reflect the tree structure of A. The modification can be done to the shared ancestor tree of A and B.

Types of git merge strategies

Merge Commits (Explicit)

Explicit merges are considered the default merge type. It is called explicit because it creates a new merge commit, changing the history and displaying where the merge was invoked. The merge commit content is also considered explicit as it displays the parent commits of the merge commit.

Fast-Forward Merges (Implicit)

Fast-forward merges do not create a merge commit. They simply move the target branch pointer forward to the latest commit of the source branch. This behavior is triggered automatically when the target branch has not diverged from the source branch.

Squash Merges

Squash is a merge option that avoids creating a merge commit. A squash merge takes all commits from the source branch and combines them into a single new commit, which is then applied to the target branch. This is typically executed using the --squash flag during a merge or interactive rebase. The commit history of the source branch becomes a single squashed commit on the target branch.

Recursive git merge strategy options

oursForces conflicted parts to be auto-resolved by favoring 'our' version. The changes from the other tree not conflicting with our side are reflected to the merge output.
theirsFavors the other merging tree in conflict resolution. There is no "theirs" merge strategy unlike "ours".
patienceSpends much time to avoid mis-merges that occur because of unimportant matching lines.
diff-algorithmInstructs "merge-recursive" to use a different diff algorithm that helps to avoid mis-merges on unimportant matching lines.
ignore-space-change / ignore-all-spaceTargets whitespace characters. Whitespace changes mixed with other changes are not ignored.
renormalizeRuns a check-out and check-in on all of the file stages when resolving a 3-way merge.
no-normalizeDisables the renormalize option.
no-renamesIgnores renamed files during merge.
find-renames=nTurns on rename detection with a similarity threshold. The default threshold is 50%.
subtreeWorks on the path metadata of the tree to make the trees match.

Practice

Practice

What are the different merge strategies in Git and their characteristics?