Lesson Completion
Back to course

Merging Branches

Beginner
12 minutes4.9Git

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:

TypeWhen It HappensWhat Git Does
Fast-forwardTarget branch hasn't moved since branch pointMoves the pointer forward — no merge commit
Three-way mergeBoth branches have new commitsCreates 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

gitGraph commit id: "A" commit id: "B" branch feature/login checkout feature/login commit id: "C" commit id: "D" checkout main commit id: "E" merge feature/login id: "Merge"

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

bash
# ─── 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 main

Fast-Forward vs Three-Way vs No-FF

StrategyCreates Merge Commit?History ShapeWhen to Use
Fast-forwardNoLinearDefault; simple feature branches
Three-wayYes (automatic)BranchedWhen both branches diverged
--no-ffYes (forced)BranchedWhen 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-ff when you want a merge commit as a record, even for simple merges.
  • Use git merge --abort if anything goes wrong during a merge.
  • Check git branch --merged to 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-ff do in git 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, use git revert -m 1 <merge-commit> to create a new commit that undoes the merge.

Topics Covered

Git BranchingGit Fundamentals

Tags

#git#merge#fast-forward#beginner-friendly

Last Updated

2026-02-12