The Hook (The "Byte-Sized" Intro)
A branch without a merge is just a notebook in a drawer — useful work that never reaches production. Merging is the moment your feature stops being "in progress" and becomes real. Git offers two flavors: the seamless fast-forward (like sliding puzzle pieces together) and the three-way merge (where Git weaves two stories into one). Both are safe, reversible, and happen in seconds.
📖 What is Merging Branches?
Merging combines the work from one branch into another. You switch to the target branch (usually main), then run git merge <source>. Git figures out the best strategy automatically.
Conceptual Clarity
Two types of merges:
| Type | When It Happens | What Git Does |
|---|---|---|
| Fast-forward | Target branch hasn't moved since branch point | Moves the pointer forward — no merge commit |
| Three-way merge | Both branches have new commits | Creates a merge commit with two parents |
Fast-forward merge:
Before: A - B (main)
\
C - D (feature)
After: A - B - C - D (main, feature)
Three-way merge:
Before: A - B - E (main)
\
C - D (feature)
After: A - B - E - F (main) ← F is the merge commit
\ /
C - D (feature)
Real-Life Analogy
- Fast-forward = You're writing a book solo. Chapter 2 just continues from Chapter 1 — no weaving needed.
- Three-way merge = Two authors wrote different chapters simultaneously. The editor (Git) combines them, creating a "merge chapter" that ties them together.
Visual Architecture
Why It Matters
- Merging is how features ship. Without it, parallel work never combines.
- Fast-forward keeps history linear and simple.
- Three-way merge preserves the fact that work happened in parallel — useful for audit trails.
- Merge commits act as documentation: "Feature X was completed and integrated here."
Code
# ─── Step 1: Switch to the target branch ───
git switch main
# ─── Step 2: Merge the feature branch ───
git merge feature/login
# Output (fast-forward):
# Updating a1b2c3d..e4f5g6h
# Fast-forward
# login.js | 25 +++++++++
# 1 file changed, 25 insertions(+)
# Output (three-way merge):
# Merge made by the 'ort' strategy.
# login.js | 25 +++++++++
# auth.js | 12 ++++++
# 2 files changed, 37 insertions(+)
# ─── Force a merge commit (even when fast-forward is possible) ───
git merge --no-ff feature/signup
# Creates a merge commit with a message — useful for tracking
# when features were integrated
# ─── Abort a merge (if something goes wrong) ───
git merge --abort
# Resets everything back to before the merge started
# ─── View the merge history ───
git log --oneline --graph -10
# Shows the branch/merge structure visually
# ─── See which branches are already merged into main ───
git branch --merged main
# Output: lists branches whose work is fully in main
# ─── See which branches have NOT been merged ───
git branch --no-merged mainFast-Forward vs Three-Way vs No-FF
| Strategy | Creates Merge Commit? | History Shape | When to Use |
|---|---|---|---|
| Fast-forward | No | Linear | Default; simple feature branches |
| Three-way | Yes (automatic) | Branched | When both branches diverged |
--no-ff | Yes (forced) | Branched | When you want to record the merge explicitly |
Key Takeaways
- Always switch to the target branch before merging:
git switch main && git merge feature. - Fast-forward merges keep history linear; three-way merges preserve parallel work context.
- Use
--no-ffwhen you want a merge commit as a record, even for simple merges. - Use
git merge --abortif anything goes wrong during a merge. - Check
git branch --mergedto see which branches are safe to delete.
Interview Prep
-
Q: What is the difference between a fast-forward merge and a three-way merge? A: A fast-forward merge occurs when the target branch has no new commits since the branch point — Git simply moves the pointer forward. A three-way merge occurs when both branches have diverged, and Git creates a new merge commit with two parents that combines both histories.
-
Q: What does
--no-ffdo ingit merge? A: It forces Git to create a merge commit even when a fast-forward is possible. This preserves the fact that a feature branch existed, creating an explicit record of when the integration happened. -
Q: How do you undo a merge that hasn't been committed yet? A: Use
git merge --abort. This resets your working tree and staging area to the state before the merge began. If the merge commit was already made, usegit revert -m 1 <merge-commit>to create a new commit that undoes the merge.