The site that you are currently viewing is a static version of the Software Factory documentation delivered with the

version v7.2.
For up-to-date documentation, see the latest version.

Merging

Types of merges and branch preparation techniques

Merge logo

Introduction

Merging is the process of combining changes from different branches into a single branch. It is a fundamental operation in any SCM workflow, allowing teams to integrate work from multiple developers. Selecting the right merge approach is crucial for maintaining a clean and understandable project history.

In the Software Factory, we use Git for source code management. The examples below show Git commands, but the concepts apply to any modern SCM system.

Merge Types

Merge types denote the various methods by which an SCM system can combine commits from divergent branches, ensuring a unified progression of project history. Understanding these merge types is crucial for maintaining a clean and manageable project repository, as each type has its own implications on the project’s commit history and branch structure.

Below, we will explore the fundamental merge types, their benefits, and potential drawbacks.

Important

It’s important not to confuse these notions with Git’s merge strategies , which are the algorithms used to resolve conflicts during merges. Merge strategies are distinct concepts, and they dictate how changes from different branches are combined.

3-way merge

Combines changes from two branches that have diverged by creating a new merge commit.
Pros: Preserves the complete history of both branches.
Cons: Can produce complex histories with many merge commits.

git checkout main
git merge --no-ff feature-branch
gitGraph
   commit id:"c1"
   branch feature
   commit id:"c2"
   commit id:"c3"
   checkout main
   merge feature id:"m"

Fast-forward merge

When the target branch has not diverged from the feature branch, Git simply moves the head of the target branch forward to the end of the feature branch.
Pros: Maintains a linear history.
Cons: Not useful if the branches have diverged.

git checkout main
git merge -ff feature-branch
gitGraph
   commit id:"c1"
   commit id:"c2"
   commit id:"c3"

Branch preparation before merge

No preprocessing

Keeps commits as is and does not alter commit history.

gitGraph
   commit id:"c1"
   commit id:"c2"
   branch feature
   commit id:"f1"
   commit id:"f2"
   checkout main
   commit id:"c3"

Rebase

Rewrites history by rebasing onto target branch, replaying commits from the feature branch onto the main branch.
Pros: Linear history simplifies tracking changes.
Cons: Can be complex and risky if there are many conflicts.

git checkout feature-branch
git rebase main
gitGraph
   commit id:"c1"
   commit id:"c2"
   commit id:"c3"
   branch feature
   commit id:"f1"
   commit id:"f2"
   checkout main

Squash

Combines all commits into one commit before merging.
Pros: Keeps the main branch history clean and readable.
Cons: Loses individual commit history from the feature branch.

# squash when merging
git merge --squash feature-branch
# Or squash with rebase commande
git rebase -i main
gitGraph
   commit id:"c1"
   commit id:"c2"
   branch feature
   commit id:"f1+f2"
   checkout main
   commit id:"c3"

Resources