Git Notes
Understand fast-forward merges in Git, when they happen, how they differ from three-way merges, and how to control merge behavior with --no-ff and --ff-only flags.
A fast-forward merge is the simplest type of merge in Git. It happens when the branch you are merging into has not diverged from the branch being merged — Git simply moves the pointer forward.
What Is a Fast-Forward Merge?
When your target branch has no new commits since the feature branch was created, Git can perform a fast-forward merge by simply moving the branch pointer ahead:
Before merge (fast-forward possible)
main → [C1] ← [C2]
\
feature [C3] ← [C4] (HEAD)
After fast-forward merge
main → [C1] ← [C2] ← [C3] ← [C4] (HEAD)
No merge commit is created. The history remains perfectly linear.
When Fast-Forward Happens
A fast-forward merge occurs when:
- The target branch has NO new commits since the source branch was created
- The source branch is directly ahead of the target branch
- There is a clear linear path from target to source
CAN fast-forward (main hasn't moved)
main [A]──[B]
\
feature [C]──[D]──[E]
CANNOT fast-forward (main has new commits)
main [A]──[B]──[F]──[G]
\
feature [C]──[D]──[E]
Performing a Fast-Forward Merge
# Start on main
git checkout main
# Merge feature branch (fast-forward if possible)
git merge feature-branchUpdating a1b2c3d..e5f6g7h Fast-forward src/feature.js | 25 +++++++++++++++++++++++++ src/utils.js | 8 ++++++++ 2 files changed, 33 insertions(+) create mode 100644 src/feature.js
Notice the Fast-forward message — no merge commit was created.
Controlling Merge Behavior
Force No Fast-Forward (--no-ff)
Creates a merge commit even when fast-forward is possible:
git merge --no-ff feature-branchMerge made by the 'ort' strategy. src/feature.js | 25 +++++++++++++++++++++++++ 2 files changed, 25 insertions(+)
Result with --no-ff:
Force Fast-Forward Only (--ff-only)
Fails if fast-forward is not possible:
git merge --ff-only feature-branch# If fast-forward is possible: Updating a1b2c3d..e5f6g7h Fast-forward src/feature.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) # If fast-forward is NOT possible: fatal: Not possible to fast-forward, aborting.
Fast-Forward vs Three-Way Merge
| Aspect | Fast-Forward | Three-Way Merge |
|---|---|---|
| Merge commit | No | Yes |
| History | Linear | Shows branch topology |
| When possible | No divergence | Always |
| Reversibility | Hard to undo feature | Easy to revert merge commit |
| Visual clarity | Flat history | Clear feature boundaries |
git log --graph | Straight line | Branch and merge visible |
Practical Example
# Create and switch to feature branch
git checkout main
git checkout -b add-footer
# Make commits on feature branch
echo "<footer>Copyright 2024</footer>" > footer.html
git add footer.html
git commit -m "Add footer HTML"
echo ".footer { padding: 20px; }" > footer.css
git add footer.css
git commit -m "Add footer styles"
# Switch back to main (no new commits here)
git checkout main
# View the situation
git log --oneline --graph --all* b3c4d5e (add-footer) Add footer styles * a1b2c3d Add footer HTML * f7e8d9c (HEAD -> main) Initial project setup
# Merge with fast-forward (default)
git merge add-footerUpdating f7e8d9c..b3c4d5e Fast-forward footer.css | 1 + footer.html | 1 + 2 files changed, 2 insertions(+) create mode 100644 footer.css create mode 100644 footer.html
# View history - perfectly linear
git log --oneline --graph* b3c4d5e (HEAD -> main, add-footer) Add footer styles * a1b2c3d Add footer HTML * f7e8d9c Initial project setup
When to Use --no-ff
Use --no-ff when you want to preserve the fact that a feature was developed on a branch:
# GitFlow style - always create merge commits for features
git checkout develop
git merge --no-ff feature/payment-gateway -m "Merge feature: payment gateway"
git log --oneline --graph* e5f6g7h (HEAD -> develop) Merge feature: payment gateway |\ | * d4e5f6g Add payment processing | * c3d4e5f Add payment form | * b2c3d4e Set up payment API |/ * a1b2c3d Previous develop commit
Benefits of --no-ff:
- Clear feature boundaries in history
- Easy to revert an entire feature with one command
git log --first-parentshows only merge commits
Configure Default Merge Behavior
# Always create merge commits (never fast-forward)
git config --global merge.ff false
# Only allow fast-forward merges (fail otherwise)
git config --global merge.ff only
# Default behavior (fast-forward when possible)
git config --global merge.ff truePer-Branch Configuration
# Configure for a specific branch
git config branch.main.mergeoptions "--no-ff"Visualizing the Difference
With Fast-Forward (default)
git log --oneline --graph* e5f6g7h (HEAD -> main) Add user profile page * d4e5f6g Add user model * c3d4e5f Update database schema * b2c3d4e Initial commit
With --no-ff
git log --oneline --graph* f6g7h8i (HEAD -> main) Merge branch 'feature/user-profile' |\ | * e5f6g7h Add user profile page | * d4e5f6g Add user model | * c3d4e5f Update database schema |/ * b2c3d4e Initial commit
Interview Questions
- What is a fast-forward merge?
A fast-forward merge occurs when the target branch has no new commits since the source branch diverged. Git simply moves the target branch pointer forward to match the source branch — no merge commit is created.
- When is a fast-forward merge NOT possible?
When both branches have new commits since they diverged (the branches have diverged). Git must create a three-way merge commit to combine the changes.
- What does
--no-ffdo and why would you use it?
--no-ff forces Git to create a merge commit even when fast-forward is possible. This preserves branch topology in history, making it easy to see which commits were part of a feature and to revert entire features.
- How do you ensure only fast-forward merges are allowed?
Use git merge --ff-only which will fail if fast-forward is not possible, or set git config merge.ff only as a default.
- In GitFlow, should you use fast-forward merges for feature branches?
No — GitFlow recommends --no-ff for merging feature branches into develop. This preserves the feature branch history and makes it easy to identify and revert complete features.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Fast-Forward Merge in Git - Clean Linear History.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Git & GitHub topic.
Search Terms
git-github, git & github, git, github, branching, and, merging, fast
Related Git & GitHub Topics