Merging Branches: Fast-Forward vs Three-Way Merges (git merge)
Merging Branches: Fast-Forward vs Three-Way Merges (git merge)
Once work on an isolated feature branch is completed and tested, it must be integrated back into the main project trunk. The process of combining disparate lines of development is called merging (git merge).
1. How Merging Works
To merge branch B into branch A:
- 1You must first switch into the receiving branch (
A). - 2Run
git merge B.
Depending on the commit history structure, Git automatically chooses between two merging strategies: Fast-Forward Merge or Three-Way Merge.
2. Strategy 1: Fast-Forward Merge
A Fast-Forward merge occurs when there is a direct linear path from the current branch tip to the incoming feature branch tip. In other words, no new commits were made on main while the feature branch was being developed!
Key Properties of Fast-Forward:
- No merge commit is created.
- Git simply moves the
[main]pointer forward along the commit chain toC4. - Fast, clean, and creates zero overhead.
Forcing a Merge Commit (--no-ff):
Some teams prefer to preserve explicit historical evidence that a branch existed:
3. Strategy 2: Three-Way Merge (Recursive / Ort)
A Three-Way Merge occurs when both branches have diverged: commits were made on main AND commits were made on feature-login at the same time:
Why is it called "Three-Way"?
Git uses three distinct snapshots to calculate the merged result:
- 1The Common Ancestor Commit (
C2): The point where the two branches split. - 2The Current Branch Tip (
C6-main). - 3The Incoming Branch Tip (
C4-feature-login).
By comparing both branch tips against their common base, Git can automatically deduce what each developer intended to change:
- If a line was changed only in
feature-loginand untouched onmain, Git applies the feature change. - If a line was changed only on
mainand untouched infeature-login, Git preserves themainchange.
Git generates a brand new Merge Commit (C7) that uniquely has two parents (C4 and C6).
4. Summary: Merge Types Comparison
| Aspect | Fast-Forward Merge | Three-Way Merge (ort/recursive) |
|---|---|---|
| Branch Topology | Linear (no divergence) | Divergent (both branches advanced) |
| New Commit Created? | No (pointer moves forward) | Yes (a dedicated merge commit with 2 parents) |
| Commit Message | Reuses existing feature commit | Automatically prompts for merge commit message |
| Conflict Potential | Low / None | Possible if same lines were edited concurrently |
Practice Quiz
git merge feature-payment?Resolving Merge Conflicts Step-by-Step with Conflict Markers
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.