Bird
Raised Fist0
Gitdevops~10 mins

Rebase vs merge mental model in Git - Visual Side-by-Side Comparison

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 - Rebase vs merge mental model
Start: Branch A and Branch B diverged
Option 1: Merge
Create a new merge commit
Branches combined with history preserved
End
Option 2: Rebase
Move Branch B commits on top of Branch A
Rewrite Branch B history
Linear history without merge commit
End
Shows two ways to combine branches: merge creates a new commit joining histories, rebase moves commits to create a straight line.
Execution Sample
Git
git checkout feature
# Option 1: git merge main
# Option 2: git rebase main
Switch to feature branch, then either merge main into it or rebase feature onto main.
Process Table
StepActionBranch State BeforeBranch State AfterResult
1Start with main and feature branches divergedmain: A-B-C feature: A-B-D-Emain: A-B-C feature: A-B-D-EBranches diverged at commit B
2Merge main into featurefeature: A-B-D-Efeature: A-B-D-E-MNew merge commit M combines C and E
3Feature branch now has merge commitfeature: A-B-D-E-Mfeature: A-B-D-E-MHistory shows branching and merging
4Reset feature branch to before mergefeature: A-B-D-E-Mfeature: A-B-D-EPrepare for rebase
5Rebase feature onto mainfeature: A-B-D-Efeature: A-B-C-D'-E'Commits D and E replayed on top of C as D' and E'
6Feature branch has linear historyfeature: A-B-C-D'-E'feature: A-B-C-D'-E'No merge commit, history looks like a straight line
7Endfeature: A-B-C-D'-E'feature: A-B-C-D'-E'Branches combined by rebase or merge
💡 Execution stops after feature branch is updated by merge or rebase.
Status Tracker
VariableStartAfter 2 (merge)After 5 (rebase)Final
feature branch commitsA-B-D-EA-B-D-E-MA-B-C-D'-E'A-B-C-D'-E'
Key Moments - 3 Insights
Why does merge create a new commit but rebase does not?
Merge combines histories by creating a new commit (row 2), while rebase moves commits to a new base, rewriting history without a merge commit (row 5).
What does it mean that rebase rewrites history?
Rebase creates new commits (D' and E') with the same changes but different parent commits, making history linear (row 5), unlike merge which preserves original commits.
When should I prefer merge over rebase?
Use merge to keep full history and show branch structure (row 3). Use rebase to keep history clean and linear (row 6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the feature branch state after step 2?
AA-B-D-E-M (with a merge commit)
BA-B-C-D'-E' (rebased commits)
CA-B-D-E (unchanged)
DA-B-C (main branch only)
💡 Hint
Check the 'Branch State After' column at step 2 in the execution table.
At which step does the feature branch history become linear?
AStep 2
BStep 5
CStep 3
DStep 1
💡 Hint
Look for when commits D and E are replayed on top of C as D' and E' in the execution table.
If you want to keep the branch history showing all merges, which command would you use?
Agit rebase main
Bgit merge main
Cgit reset main
Dgit checkout main
💡 Hint
Refer to the action in step 2 where a merge commit is created.
Concept Snapshot
Rebase vs Merge in Git:
- Merge combines branches by creating a new commit.
- Rebase moves commits to a new base, rewriting history.
- Merge preserves branch history and shows merges.
- Rebase creates a linear history without merge commits.
- Use merge for shared branches, rebase for clean history.
Full Transcript
This visual shows how Git combines branches using merge or rebase. Initially, main and feature branches diverge at commit B. Using merge, a new commit M joins the histories, preserving the branch structure. Using rebase, commits from feature are moved on top of main's latest commit C, creating new commits D' and E' and a straight history line. Merge keeps all commits and shows the branch split and join. Rebase rewrites commits to make history linear without merge commits. Choose merge to keep full history or rebase to keep history clean.

Practice

(1/5)
1. What is the main difference between git merge and git rebase?
easy
A. git merge rewrites commit messages; git rebase preserves commit messages.
B. git merge deletes the source branch; git rebase deletes the target branch.
C. git merge only works on remote branches; git rebase only works on local branches.
D. git merge combines histories preserving all commits; git rebase rewrites history to create a linear sequence.

Solution

  1. Step 1: Understand git merge behavior

    git merge combines two branches by creating a new commit that preserves the history of both branches without changing existing commits.
  2. Step 2: Understand git rebase behavior

    git rebase moves or reapplies commits from one branch onto another, rewriting history to make it look like a straight line.
  3. Final Answer:

    git merge combines histories preserving all commits; git rebase rewrites history to create a linear sequence. -> Option D
  4. Quick Check:

    Merge preserves history, rebase rewrites it [OK]
Hint: Merge keeps history; rebase rewrites it linearly [OK]
Common Mistakes:
  • Thinking merge deletes branches
  • Believing rebase only works on remote branches
  • Confusing which command rewrites history
2. Which of the following is the correct syntax to rebase the current branch onto main?
easy
A. git rebase main
B. git merge main
C. git rebase origin/main
D. git checkout main && git rebase

Solution

  1. Step 1: Identify the command to rebase current branch

    The command git rebase main rebases the current branch onto the main branch.
  2. Step 2: Check other options for correctness

    git merge main merges, not rebases; git rebase origin/main rebases onto remote tracking branch which may be outdated; git checkout main && git rebase is invalid syntax.
  3. Final Answer:

    git rebase main -> Option A
  4. Quick Check:

    Rebase current branch onto main = git rebase main [OK]
Hint: Use 'git rebase branch-name' to rebase current branch [OK]
Common Mistakes:
  • Using merge instead of rebase
  • Rebasing onto remote branch without fetching
  • Incorrect chaining of commands
3. Given the following commands executed in order on branch feature:
git checkout feature
git rebase main
git log --oneline --graph
What will the commit history look like compared to using git merge main instead?
medium
A. A linear history with feature commits on top of main commits.
B. A merge commit combining main and feature histories.
C. No change in history; feature branch remains separate.
D. Feature branch commits are deleted.

Solution

  1. Step 1: Understand effect of git rebase main on feature branch

    Rebasing moves feature commits to be based on the latest main commits, creating a straight, linear history.
  2. Step 2: Compare with git merge main effect

    Merging creates a new merge commit that combines histories, preserving the branch structure and showing a branch point.
  3. Final Answer:

    A linear history with feature commits on top of main commits. -> Option A
  4. Quick Check:

    Rebase = linear history; merge = merge commit [OK]
Hint: Rebase = linear history; merge = merge commit [OK]
Common Mistakes:
  • Thinking rebase creates merge commits
  • Believing history stays unchanged after rebase
  • Assuming commits are deleted after rebase
4. You ran git rebase main on your feature branch but got conflicts. After resolving conflicts, which command should you run to continue the rebase?
medium
A. git commit -m 'resolved conflicts'
B. git merge --continue
C. git rebase --continue
D. git rebase --abort

Solution

  1. Step 1: Identify the correct command to continue rebase after conflicts

    After resolving conflicts during a rebase, git rebase --continue tells Git to proceed with applying remaining commits.
  2. Step 2: Understand other options

    git merge --continue is for merge conflicts, not rebase; git commit -m is manual commit but rebase expects --continue; git rebase --abort cancels the rebase.
  3. Final Answer:

    git rebase --continue -> Option C
  4. Quick Check:

    Continue rebase after conflicts = git rebase --continue [OK]
Hint: Use 'git rebase --continue' after resolving conflicts [OK]
Common Mistakes:
  • Using merge commands during rebase
  • Trying to commit manually instead of continuing
  • Aborting instead of continuing rebase
5. You want to update your feature branch with the latest changes from main but keep a clean, linear history without merge commits. Which sequence of commands achieves this safely?
hard
A. git checkout feature; git merge origin/main
B. git checkout feature; git fetch origin; git rebase origin/main
C. git checkout main; git pull; git checkout feature; git merge main
D. git checkout feature; git pull origin main

Solution

  1. Step 1: Fetch latest changes from remote main branch

    git fetch origin updates local remote tracking branches without changing working branches.
  2. Step 2: Rebase feature branch onto updated origin/main

    git rebase origin/main reapplies feature commits on top of latest main commits, keeping history linear and clean.
  3. Final Answer:

    git checkout feature; git fetch origin; git rebase origin/main -> Option B
  4. Quick Check:

    Fetch then rebase for clean update [OK]
Hint: Fetch first, then rebase onto remote main for clean history [OK]
Common Mistakes:
  • Merging instead of rebasing for linear history
  • Pulling directly on feature branch causing merge commits
  • Not fetching latest remote changes before rebase