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 mergeIn 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


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
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

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 Forward | Recursive |
---|
No new commits on the master | New commits on the master |
Linear History | Commit 2 parents |
No merge commits | Merge commit is created |
git rebase | git 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 MergeOctopus 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

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.
Similar Reads
DevOps Tutorial
DevOps is a combination of two words: "Development" and "Operations." Itâs a modern approach where software developers and software operations teams work together throughout the entire software life cycle, from planning and coding to testing, deploying, and monitoring.The main idea of DevOps is to i
9 min read
Introduction
What is DevOps ?
As technology keeps advancing, businesses need to deliver software faster and more reliably. DevOps helps achieve this by bringing together development and operations teams, ensuring they work as one instead of in separate silos.DevOps is more than just a set of toolsâitâs a way of working that prom
11 min read
DevOps Lifecycle
DevOps is a practice that enables a single team to handle the whole application lifecycle, including development, testing, release, deployment, operation, display, and planning. It is a mix of the terms "Dev" (for development) and "Ops" (for operations). We can speed up the delivery of applications
6 min read
The Evolution of DevOps - 3 Major Trends for Future
DevOps is a software engineering culture and practice that aims to unify software development and operations. It is an approach to software development that emphasizes collaboration, communication, and integration between software developers and IT operations. DevOps has come a long way since its in
7 min read
Continuous Integration (CI) & Continuous Deployment (CD)
Infrastructure as Code (IaC)
Microsoft Teams vs Slack
Both Microsoft Teams and Slack are the communication channels used by organizations to communicate with their employees. Microsoft Teams was developed in 2017 whereas Slack was created in 2013. Microsoft Teams is mainly used in large organizations and is integrated with Office 365 enhancing the feat
4 min read