Merging Branches: Fast-Forward vs Three-Way Merges (git merge)0%

Merging Branches: Fast-Forward vs Three-Way Merges (git merge)

Beginner12 min read•Updated: Jul 11, 2026
Study MaterialsCheatsheet

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:

  1. 1
    You must first switch into the receiving branch (A).
  2. 2
    Run git merge B.
Bash / Terminal
# 1. Switch to destination branch
git switch main
 
# 2. Merge the feature branch into main
git merge feature-login

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!

Output
Before Fast-Forward Merge:
(C1) <--- (C2) [main, HEAD]
\
(C3) <--- (C4) [feature-login]
 
Command: git switch main && git merge feature-login
 
After Fast-Forward Merge:
(C1) <--- (C2) <--- (C3) <--- (C4) [feature-login, main, HEAD]

Key Properties of Fast-Forward:

  • No merge commit is created.
  • Git simply moves the [main] pointer forward along the commit chain to C4.
  • Fast, clean, and creates zero overhead.

Forcing a Merge Commit (--no-ff):

Some teams prefer to preserve explicit historical evidence that a branch existed:

Bash / Terminal
git merge --no-ff feature-login

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:

Output
Diverged History Before Merge:
(C3) <--- (C4) [feature-login]
/
(C1) <--- (C2) [Common Ancestor Base]
\
(C5) <--- (C6) [main, HEAD]

Why is it called "Three-Way"?

Git uses three distinct snapshots to calculate the merged result:

  1. 1
    The Common Ancestor Commit (C2): The point where the two branches split.
  2. 2
    The Current Branch Tip (C6 - main).
  3. 3
    The 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-login and untouched on main, Git applies the feature change.
  • If a line was changed only on main and untouched in feature-login, Git preserves the main change.
Output
After Three-Way Merge:
(C3) <--- (C4) [feature-login]
/ \
(C1) <--- (C2) (C7) [Merge Commit! main, HEAD]
\ /
(C5) <--- (C6)

Git generates a brand new Merge Commit (C7) that uniquely has two parents (C4 and C6).


4. Summary: Merge Types Comparison

AspectFast-Forward MergeThree-Way Merge (ort/recursive)
Branch TopologyLinear (no divergence)Divergent (both branches advanced)
New Commit Created?No (pointer moves forward)Yes (a dedicated merge commit with 2 parents)
Commit MessageReuses existing feature commitAutomatically prompts for merge commit message
Conflict PotentialLow / NonePossible if same lines were edited concurrently

Interactive Quiz

Practice Quiz

Question 1
What must you do before running git merge feature-payment?
Question 2
When does Git perform a Fast-Forward merge?
Question 3
What unique property distinguishes a Merge Commit resulting from a three-way merge?
Question 4
Which flag forces Git to generate a dedicated merge commit even if a fast-forward merge was possible?
Question 5
In a three-way merge, which three snapshots are evaluated to determine the merge result?
Next Lesson

Resolving Merge Conflicts Step-by-Step with Conflict Markers

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

PrevNext