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
Fast-forward merge
📖 Scenario: You are working on a project using Git. You have a main branch called main and a feature branch called feature. You want to practice merging the feature branch into main using a fast-forward merge.
🎯 Goal: Learn how to create branches, make commits, and perform a fast-forward merge in Git.
📋 What You'll Learn
Create a branch called feature from main
Make a commit on the feature branch
Switch back to main branch
Perform a fast-forward merge of feature into main
Show the commit log to confirm the merge
💡 Why This Matters
🌍 Real World
Fast-forward merges are common when a feature branch is directly ahead of the main branch without any divergent commits. This keeps the commit history clean and linear.
💼 Career
Understanding fast-forward merges is essential for software developers and DevOps engineers to manage code changes efficiently and maintain a clear project history.
Progress0 / 4 steps
1
Create the feature branch from main
Run the command git branch feature to create a new branch called feature from the current main branch.
Git
Hint
Use git branch feature to create the branch.
2
Switch to the feature branch and make a commit
Run git checkout feature to switch to the feature branch. Then create a file called feature.txt with the content Feature work. Stage the file with git add feature.txt and commit with the message add feature work.
Git
Hint
Switch branches with git checkout feature. Use echo to create the file, then add and commit it.
3
Switch back to the main branch
Run git checkout main to switch back to the main branch.
Git
Hint
Use git checkout main to switch branches.
4
Perform a fast-forward merge and show the commit log
Run git merge feature to merge the feature branch into main using a fast-forward merge. Then run git log --oneline --graph --all to display the commit history and confirm the merge.
Git
Hint
Use git merge feature to merge and git log --oneline --graph --all to see the commits.
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
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.
Step 2: Compare with other merge types
Unlike a normal merge, it does not create a new commit and keeps history linear.
Final Answer:
The branch pointer moves forward without creating a new commit. -> Option C
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
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.
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.
Final Answer:
git merge --ff-only feature -> Option D
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
Step 1: Understand branch states before merge
Main points to commit A. Feature branch adds commit B on top of A.
Step 2: Analyze merge behavior
Since main has no new commits after branching, merging feature into main will fast-forward main to B.
Final Answer:
B\nA -> Option B
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
Step 1: Understand --ff-only error cause
The --ff-only option fails if a fast-forward merge is not possible.
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.
Final Answer:
The main branch has new commits not in feature. -> Option A
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
Step 1: Understand the problem with fast-forward
Since both branches have new commits, a fast-forward merge is not possible directly.
Step 2: Use rebase to linearize history
Rebasing feature onto main moves feature commits on top of main, enabling a fast-forward merge afterward.
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.
Final Answer:
Use git rebase main on feature, then fast-forward merge. -> Option A
Quick Check:
Rebase then merge = linear history [OK]
Hint: Rebase feature on main to enable fast-forward merge [OK]