Open In App

Merge Strategies in Git

Last Updated : 15 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Git, merging is the process of taking the changes from one branch and combining them into another. The merge command in Git will compare the two branches and merge them if there are no conflicts. If conflicts arise, Git will ask the user to resolve them before completing the merge.

  • Merge keeps all changes and shows a merge commit in the history.
  • It can merge more than two branches at once.
  • Works even if the branches have different changes.
  • Adds a special commit to show when the branches were combined.

Common Merge Strategies

Git provides several merge strategies, each suited for different scenarios. The choice of strategy depends on the complexity of changes and the desired outcome. Here are the most commonly used merge strategies:

1. Fast Forward Merge

A fast-forward merge occurs when the target branch has not diverged from the source branch. In this case, Git simply moves the target branch pointer to the latest commit in the source branch. This strategy is simple and keeps the commit history linear.

Fast-Forward-Merge
Fast-forward merge

In this most commonly used merge strategy, history is just one straight line. When you create a branch, make some commits in that branch, the time you're ready to merge, there is no new merge on the master. That way master's pointer is just moved straight forward and history is one straight line.

Command:

git checkout main
git merge feature-branch
FF Merge StrategyFF Merge Strategy

2. Recursive Merge (Default)

Recursive merge is Git's default strategy for non-trivial merges. It handles cases where branches have diverged by creating a new merge commit. This commit records the combined changes from both branches, preserving the history of both lines of development.

Recursive-Merge
Recursive Merge


In Recursive merge, after you branch and make some commits, there are some new original commits on the 'master'. So, when it's time to merge, git recurses over the branch and creates a new merge commit. The merge commit continues to have two parents.

Command:

$ git merge--no-ff
Recursive-merge-strategy

Note: There is nothing right or wrong of either one of the strategies but with fast forward merge you have a straight line of history and with the recursive merge, it is of multiple lines.

Fast-Forward merge vs Recursive merge

Fast ForwardRecursive
No new commits on the masterNew commits on the master
Linear HistoryCommit 2 parents
No merge commitsMerge commit is created
git rebasegit merge--no-ff

3. Octopus Merge

Octopus merge is used for merging more than two branches simultaneously. It’s less common and typically used for automated merges involving multiple feature branches.

Octopus-Merge-Strategy
Octopus Merge

Octopus Merge strategy resolves cases with more than two heads but refuses to do a complex merge that needs manual resolution. It is primarily meant to be used for bundling topic branch heads together. This is the default merge strategy when pulling or merging more than one branch.

Command:

$ git merge -s octopus
Octopus-merge-Strategy-Example

4. Subtree Merge

The subtree merge strategy is useful when you want to merge a project into a subdirectory of another project. This is typically used for merging external projects or submodules into your project.

Use Case: Use the subtree strategy when merging an entire project into a subdirectory of the current repository.

Command:

git merge -s subtree branch-name

5. Squash and Merge

Squash and merge squashes all the commits from a feature branch into a single commit before merging into the target branch. This strategy simplifies the commit history, making it easier to follow.

Use Case: Ideal for merging feature branches with numerous small commits, resulting in a cleaner main branch history.

Command:

git checkout main
git merge --squash feature-branch
git commit -m "Merged feature-branch with squash"

6. Ours Merge

The ours strategy is used when you want to keep the changes from the current branch (the branch you are merging into) and discard the changes from the branch being merged.

Use Case: Use the ours strategy when you want to discard changes from the branch being merged and keep your own.

Command:

git merge -s ours branch-name

When to Use Different Merge Strategies

Merge Strategy

When to Use

Fast Forward Merge

When there are no new commits in the target branch.

Recursive Merge

When two branches have independent changes and need to be merged.

Octopus Merge

When you need to merge multiple branches at once.

Subtree Merge

When you want to merge a project or submodule into a subdirectory.

Squash and Merge

When you want to combine multiple small commits into a single commit.

Ours Merge

When you want to keep the current branch’s changes and discard the other branch’s.

Conclusion

Merge strategies in Git offer different ways of combining changes from multiple branches, and each strategy has its specific use cases. Whether you want to preserve commit history, discard changes, or ensure a cleaner history, Git’s merge strategies help you manage and organize your repository efficiently.


Next Article
Article Tags :

Similar Reads