Class 16 of 50 • Branching & Merging

Git Merging Strategies: Fast-Forward Merges vs Three-Way Recursive Merges

Merging is the process of integrating independent lines of development into a unified branch. When you run git merge, Git analyzes the commit graph topology and automatically selects the optimal merge strategy: Fast-Forward or Three-Way Recursive Merge.

Strategy 1: The Fast-Forward Merge (Linear Advancement)

A Fast-Forward merge occurs when the target branch (e.g. main) has received NO new commits since you branched off it. In this scenario, there are no divergent changes to reconcile.

To merge, Git doesn't need to create a new merge commit. It simply moves (fast-forwards) the main branch pointer forward to point to the latest commit of your feature branch!

# Step 1: Switch to the branch you want to merge INTO
$ git switch main

# Step 2: Merge the feature branch
$ git merge feature/login
Updating 4a1b2c..8d9e0f
Fast-forward
 src/login.js | 45 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)

Disabling Fast-Forward: Forcing a Merge Commit (--no-ff)

Many production teams prefer to maintain a record of feature branch lifecycles in the commit graph. You can force Git to create a dedicated merge commit even when a fast-forward is possible:

$ git merge --no-ff feature/login -m "merge: integrate feature/login"

Strategy 2: The Three-Way Merge (Divergent History)

A Three-Way Merge occurs when both branches have diverged—i.e., new commits were added to main while you were working on feature/login.

Git cannot simply fast-forward the pointer. Instead, Git's merge engine (the modern ort strategy) performs a 3-way calculation using three snapshots:

  1. Common Ancestor: The last commit both branches shared before diverging.
  2. Branch 1 Tip (HEAD): The latest commit on main.
  3. Branch 2 Tip: The latest commit on feature/login.

Git compares the changes made on both branches relative to the common ancestor. If the changes occurred in different files or non-overlapping lines of code, Git automatically merges them and creates a brand-new Merge Commit with two parent commits!

$ git merge feature/login
Merge made by the 'ort' strategy.
 auth.js | 12 ++++++++++++
 1 file changed, 12 insertions(+)

BCA University Viva Question

Question: "What makes a merge commit special in Git?"
Answer: "A standard commit has exactly one parent commit. A merge commit created by a 3-way merge has two (or more) parent commits, documenting the exact historical point where two independent branches converged."

Explore Related Git & GitHub Tutorials & Next Steps

Strengthen your version control mastery by studying these connected classes:

Solving Merge Conflicts

Step-by-step conflict resolution workflow using diff markers and graphical tools.

git rebase vs git merge

Compare merge commits with rebased linear commit streams.

GitHub PR Merge Strategies

Compare GitHub's Merge Commit, Squash & Merge, and Rebase & Merge buttons.

Branching Architecture

Learn how branch pointers diverge and converge in the Directed Acyclic Graph.

Frequently Asked Questions (FAQ) - Student & Interview Review

`ort` stands for 'Ostensibly Recursive's Twin'. Introduced in Git 2.33, it is the modern rewrite of the classic `recursive` merge engine. It resolves complex merges significantly faster and handles renames with greater accuracy.
Yes! If you encounter merge issues before finalizing, run `git merge --abort`. Git will cleanly restore your working directory and branch to the exact state prior to running the merge command.
An octopus merge is a merge that combines more than two branches simultaneously in a single merge commit. It is used in large projects to merge multiple clean topic branches.

Next Step in Your Git & GitHub Learning Path:

Advance your software engineering skills with the next topic in our synchronized curriculum.

Continue to Merge Conflicts → Previous: ← git switch & checkout | Full Syllabus

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Reddit

You may also like this!