# Fast-Forward vs 3-Way Merges

> Understand how Git combines histories: sliding a pointer forward vs forging a new merge commit.

Subject: Version Control | Lesson 11 of 29 | 5 min read
Interactive lesson: https://codewithanimation.in/git/fast-forward-vs-3-way
Previous: [Merging Basics](https://codewithanimation.in/git/git-merge.md) | Next: [Resolving Merge Conflicts](https://codewithanimation.in/git/resolving-conflicts.md)

## Two ways to merge

A **Fast-Forward Merge** occurs when the current branch's tip is a direct ancestor of the merging branch. Git simply moves (fast-forwards) the current branch pointer forward to the merging branch's tip without creating a new merge commit.

A **3-Way Merge** occurs when the commit history has diverged. Git uses three commits (the two branch tips and their common ancestor) to create a new, distinct 'merge commit' that integrates the divergent histories together.

## Why it matters

Fast-Forward merges keep the Git history perfectly linear. This makes it easier to read the log sequentially and find regressions using tools like `git bisect`.

3-Way Merges preserve the context of a feature. It clearly groups related commits together and provides a dedicated merge commit that answers 'when and where was this feature introduced?'.

## How it works

In a linear history, no new commits were added to the base branch while the feature branch was developed. Git just slides the pointer up the line.

In a divergent history, commits were added to both the base branch and the feature branch since they split. Git must compare both tips against their common base to resolve changes safely.

## Common mistake: Losing Context

The #1 common mistake is defaulting exclusively to fast-forward merges on long-running features.

This causes developers to lose branch context, leading to a messy, indistinguishable linear history where feature commits are tangled up with mainline hotfixes and minor tweaks, making rollbacks painful.

## Tip: Force a merge commit with --no-ff

Use the `--no-ff` (no fast-forward) flag when merging completed feature branches into main.

Think of `--no-ff` as a wrapper. It forces a 3-way merge commit even if a fast-forward is possible, effectively turning your Git history into a readable timeline of *completed features* rather than just a massive list of individual *saves*.

## Try it: Compare two merge histories

Switch between a linear history and a divergent history to compare their merge results.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/git/fast-forward-vs-3-way

## Check yourself

### Question 1

When will Git automatically perform a fast-forward merge by default?

- A. When you use the `--no-ff` flag.
- B. When the branch being merged into has not received any new commits since the feature branch was created.
- C. When the two branches have diverged and both have new commits.
- D. When there are merge conflicts.

Correct answer: B

A fast-forward merge only happens when the history is strictly linear. If the receiving branch hasn't moved, Git just moves the pointer forward to the new tip.

- Why not A: `--no-ff` specifically prevents a fast-forward merge, forcing a merge commit instead.
- Why not C: This scenario requires a 3-way merge, as the history is not linear.
- Why not D: Merge conflicts only happen in 3-way merges where histories diverged. Fast-forwards never have conflicts.

### Question 2

What is the primary benefit of forcing a merge commit (using `--no-ff`) even when a fast-forward is possible?

- A. It automatically resolves all merge conflicts.
- B. It preserves the context that a group of commits belonged together as a specific feature.
- C. It makes the git history perfectly linear.
- D. It deletes the feature branch automatically.

Correct answer: B

By forcing a merge commit, you create a dedicated node in the history that says 'this feature was merged here,' keeping the related commits logically grouped together.

- Why not A: Forcing a merge commit does not magically resolve conflicts; they still need manual resolution if histories diverged.
- Why not C: A merge commit actually creates a 'bubble' in the history graph, making it non-linear.
- Why not D: Branch deletion must be done manually with `git branch -d` regardless of the merge type.

### Question 3

In a 3-way merge, what are the '3 ways' (or 3 commits) Git looks at to combine changes safely?

- A. The three most recent commits on the feature branch.
- B. Main, development, and the feature branch.
- C. The local branch, the remote origin branch, and the upstream branch.
- D. The tip of the current branch, the tip of the merging branch, and their most recent common ancestor.

Correct answer: D

To safely combine diverged histories, Git compares the current state of both branches against the last point where they were identical (the common ancestor).

- Why not A: Git needs to look at both branches to merge, not just the recent history of one.
- Why not B: Git only merges two branches at a time, not three distinct branches.
- Why not C: This refers to remote tracking, but a local merge operates on two specific branch tips and their ancestor.

## Remember this

- A fast-forward merge simply moves the branch pointer forward.
- A 3-way merge creates a brand new merge commit.
- Use `--no-ff` to force a merge commit and preserve the feature's context.
