Bird
Raised Fist0
Gitdevops~10 mins

Fast-forward merge in Git - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Process Flow - Fast-forward merge
Start on main branch
Create feature branch
Make commits on feature branch
Switch back to main branch
Check if main is behind feature
Yes
Fast-forward merge: move main pointer to feature
Main branch updated, no merge commit
End
This flow shows how a fast-forward merge moves the main branch pointer forward to the feature branch commit when no divergent commits exist.
Execution Sample
Git
git checkout main

git checkout -b feature

git commit -m "Add feature"

git checkout main

git merge feature
This sequence creates a feature branch, commits to it, then merges it back into main using fast-forward.
Process Table
StepCommandBranch HEADActionResult
1git checkout mainmainSwitch to main branchHEAD points to main branch tip
2git checkout -b featurefeatureCreate and switch to feature branchfeature branch created at main tip, HEAD points to feature
3git commit -m "Add feature"featureCommit on feature branchfeature branch moves forward by one commit
4git checkout mainmainSwitch back to main branchHEAD points to main branch tip (behind feature)
5git merge featuremainFast-forward mergemain branch pointer moves to feature commit, no merge commit created
💡 Fast-forward merge completes because main branch has no new commits since feature branch was created.
Status Tracker
VariableStartAfter 1After 2After 3After 4After 5
HEADmainmainfeaturefeaturemainmain
main branch pointercommit0commit0commit0commit0commit0commit1 (feature commit)
feature branch pointercommit0commit0commit1commit1commit1commit1
Key Moments - 3 Insights
Why does the merge not create a new commit?
Because main branch has no new commits after branching, git just moves main pointer forward to feature commit (see execution_table step 5).
What happens if main had new commits after feature branch was created?
Fast-forward merge would not be possible; git would create a merge commit to combine histories.
Does fast-forward merge change the commit history?
No, it only moves the branch pointer forward without creating new commits, preserving a linear history.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the branch HEAD after step 3?
Afeature
Bmain
Cdetached HEAD
Dunknown
💡 Hint
Check the 'Branch HEAD' column at step 3 in the execution_table.
At which step does the main branch pointer move to the feature commit?
AStep 3
BStep 4
CStep 5
DIt never moves
💡 Hint
Look at the 'Result' column in execution_table for when main branch pointer updates.
If main had new commits after branching, what would change in the execution table?
AHEAD would point to detached state
BStep 5 would create a merge commit instead of fast-forward
CStep 3 would fail
DFeature branch would be deleted
💡 Hint
Refer to key_moments about when fast-forward merge is possible.
Concept Snapshot
Fast-forward merge moves the branch pointer forward when no divergent commits exist.
Syntax: git merge <branch>
If main is behind feature, main pointer moves to feature commit.
No merge commit is created.
Preserves linear history.
Only works if main has no new commits since branching.
Full Transcript
Fast-forward merge happens when you merge a branch that is ahead of your current branch without any new commits on the current branch. You start on main, create a feature branch, make commits there, then switch back to main. When you run git merge feature, git moves the main branch pointer forward to the feature commit because main has no new commits. This merge does not create a new commit, keeping history linear. If main had new commits, git would create a merge commit instead. This process is simple and keeps your commit history clean.

Practice

(1/5)
1. What happens during a fast-forward merge in Git?
easy
A. The source branch is deleted automatically.
B. A new merge commit is always created.
C. The branch pointer moves forward without creating a new commit.
D. The commit history becomes non-linear.

Solution

  1. Step 1: Understand fast-forward merge behavior

    A fast-forward merge moves the branch pointer forward to the latest commit of the source branch without creating a new merge commit.
  2. Step 2: Compare with other merge types

    Unlike a normal merge, it does not create a new commit and keeps history linear.
  3. Final Answer:

    The branch pointer moves forward without creating a new commit. -> Option C
  4. Quick Check:

    Fast-forward merge = pointer moves forward [OK]
Hint: Fast-forward means no new commit, just pointer moves [OK]
Common Mistakes:
  • Thinking a merge commit is always created
  • Assuming source branch deletes automatically
  • Believing history becomes non-linear
2. Which Git command syntax performs a fast-forward merge of branch feature into main only if possible, otherwise aborts?
easy
A. git merge --squash feature
B. git merge feature
C. git merge --no-ff feature
D. git merge --ff-only feature

Solution

  1. Step 1: Identify command for fast-forward only

    The option --ff-only tells Git to merge only if it can fast-forward, otherwise it aborts.
  2. Step 2: Compare other options

    --no-ff disables fast-forward, --squash creates a single commit without merging, and plain git merge feature may create a merge commit.
  3. Final Answer:

    git merge --ff-only feature -> Option D
  4. Quick Check:

    Fast-forward only = --ff-only [OK]
Hint: Use --ff-only to ensure only fast-forward merges happen [OK]
Common Mistakes:
  • Using --no-ff disables fast-forward merges
  • Assuming plain merge always fast-forwards
  • Confusing --squash with fast-forward
3. Given the following Git commands, what is the output of git log --oneline main after merging?
git checkout main
# main points to commit A

git checkout -b feature
# feature branch created from A

git commit --allow-empty -m "Add feature commit"
# feature now points to commit B

git checkout main

git merge feature
medium
A. B
B. B\nA
C. A
D. Merge commit with A and B

Solution

  1. Step 1: Understand branch states before merge

    Main points to commit A. Feature branch adds commit B on top of A.
  2. Step 2: Analyze merge behavior

    Since main has no new commits after branching, merging feature into main will fast-forward main to B.
  3. Final Answer:

    B\nA -> Option B
  4. Quick Check:

    Fast-forward merge moves main to B [OK]
Hint: If main unchanged, merge moves pointer to feature commit [OK]
Common Mistakes:
  • Expecting merge commit creation
  • Thinking main stays at A
  • Confusing commit hashes output
4. You tried to fast-forward merge branch feature into main using git merge --ff-only feature, but Git returned an error. What is the most likely cause?
medium
A. The main branch has new commits not in feature.
B. The feature branch is behind main.
C. The feature branch has no commits.
D. You forgot to commit changes on feature branch.

Solution

  1. Step 1: Understand --ff-only error cause

    The --ff-only option fails if a fast-forward merge is not possible.
  2. Step 2: Identify when fast-forward is impossible

    If main has new commits not in feature, Git cannot fast-forward main to feature, causing the error.
  3. Final Answer:

    The main branch has new commits not in feature. -> Option A
  4. Quick Check:

    Fast-forward fails if main has new commits [OK]
Hint: Fast-forward fails if main moved ahead since branching [OK]
Common Mistakes:
  • Assuming feature must be behind main
  • Thinking empty feature branch causes error
  • Confusing uncommitted changes with merge errors
5. You have a main branch and a feature branch. Both have new commits since branching. You want to merge feature into main but keep history linear without merge commits. Which approach is best?
hard
A. Use git rebase main on feature, then fast-forward merge.
B. Use git merge --no-ff feature to force a merge commit.
C. Use git merge --ff-only feature and abort if not fast-forward.
D. Delete main and rename feature to main.

Solution

  1. Step 1: Understand the problem with fast-forward

    Since both branches have new commits, a fast-forward merge is not possible directly.
  2. Step 2: Use rebase to linearize history

    Rebasing feature onto main moves feature commits on top of main, enabling a fast-forward merge afterward.
  3. Step 3: Perform fast-forward merge after rebase

    After rebase, merging feature into main will be a fast-forward, keeping history linear without merge commits.
  4. Final Answer:

    Use git rebase main on feature, then fast-forward merge. -> Option A
  5. Quick Check:

    Rebase then merge = linear history [OK]
Hint: Rebase feature on main to enable fast-forward merge [OK]
Common Mistakes:
  • Trying --ff-only merge when not possible
  • Forcing merge commit breaks linear history
  • Deleting branches unnecessarily