What's the Best Git Merge Strategy?

What's the Best Git Merge Strategy?

Julien Danjou

Once developers have completed work on project features and you’ve approved their pull requests, it’s time to merge those requests into the main project repository.

When doing so, Git has to find a base commit shared by two or more commits and then connect the two branches. Git has several ways to go about this, known as merge strategies.

What is a git merge strategy?

Git offers plenty of ways to find a base commit shared by commit pointers when you need to perform a git merge. These are called merge strategies in Git.

Once a common base commit is found, Git combines the base commits shared by the commit pointers into a new merge commit. This merge commit is almost exactly like the regular commits—except that it has two parent commits.

Best git merge strategies

Recursive merge

Main advantage: Simple and easy for two-way merges.

Recursive merges are the default for any merges that aren’t fast-forward merges (covered later). These types of merges operate on two different heads using a three-way merge algorithm. The merge commit ends up having two parents once the merge is complete.

The recursive strategy can detect and handle merges that involve renaming, but it can’t make use of detected copies.

Resolve merge

Main advantage: Helps with criss-cross merges and complex “regular” merges.

Resolve is generally considered a safe and fast merge strategy. However, it can only resolve two heads—your current branch and the one you’re pulling from—with a three-way merge algorithm.

Resolve is suitable for criss-cross merge situations as well as “regular” merges where the merge history might be complex.

Fast-forward merge

Main advantage: Fast and clean if there’s a linear path to the target branch.

As mentioned, the fast-forward merge strategy is a type of implicit merge. It’s used quite commonly, thanks to its simplicity.

Fast-forward merge

In fact, there’s technically no “merging” happening here. This merge strategy can be used when there’s a linear path from the tip of the current branch all the way to the target branch. All it does is move the current branch’s tip up to the target branch’s tip, hence the “fast-forward” moniker.

By doing so, both histories are combined since you can reach any of the target branch’s commits through the current branch.

Octopus merge

Main advantage: Merging multiple branches for similar features into another branch.

The octopus merge is the default git merge strategy when you’re dealing with more than two heads—or, in other words, you’re merging more than one branch into another branch.

Octopus merge with 2 feature branches

However, octopus merges refuse the attempted merge if any merge conflicts require manual resolution.

Consequently, octopus merges are mostly used for bundling branch heads for similar features into one merge.

Ours merge

Main advantage: Faster merging of multiple branches that were built for unneeded features.

Like the octopus merge, the ours merge strategy can deal with any number of branches. However, when you perform the ours merge, the resulting tree will always be that of the current branch head. That means changes on all the other branches have been ignored (that’s where the “ours” comes from).

Ours can be used to supersede old development histories from unneeded feature branches.

Subtree merge

Main advantage: Good for managing multiple repositories in one.

The subtree merge modifies the recursive merge strategy. It's best explained with an example.

Let’s say you attempt to merge tree X with tree Y. If tree Y corresponds with one of tree X’s subtrees—making it a tree X child—tree Y is changed to match up with tree X’s structure.

This merge strategy also applies this adjustment to tree X’s and tree Y’s common ancestor tree.

Those are only some of the best git merge strategies. There are plenty more.

If you’re struggling to figure out merge strategies and which ones to use, consider instead getting a GitHub bot like Mergify.

Let Mergify handle it for you

Mergify can automatically perform various actions for you, from merging to backporting to assigning reviewers and more. All you have to do is write your rules in YAML, and Mergify handles it all for you while providing you real-time reporting on pull request status.

Mergify is free for GitHub users and takes only seconds to install. Get started with Mergify today!