Bird
Raised Fist0
Gitdevops~7 mins

Rebase vs merge mental model in Git - CLI 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
Introduction
When working with Git, you often need to combine changes from different branches. Rebase and merge are two ways to do this, but they work differently. Understanding when to use each helps keep your project history clean and easy to follow.
When you want to update your feature branch with the latest changes from the main branch before finishing your work
When you want to combine changes from two branches but keep a clear history of how the work was done
When you want to avoid creating extra merge commits to keep the project history linear
When you want to preserve the exact history of how branches were combined, including merge points
When you want to prepare your branch for a clean integration into the main branch
Commands
Switch to your feature branch where you want to integrate changes from the main branch.
Terminal
git checkout feature-branch
Expected OutputExpected
Switched to branch 'feature-branch'
Rebase your feature branch on top of the latest main branch commits. This moves your changes to the tip of main, creating a linear history.
Terminal
git rebase main
Expected OutputExpected
First, rewinding head to replay your work on top of it... Applying: Add new feature
Switch back to the main branch to prepare for merging your feature branch.
Terminal
git checkout main
Expected OutputExpected
Switched to branch 'main'
Merge the feature branch into main. This creates a merge commit that shows the branches were combined.
Terminal
git merge feature-branch
Expected OutputExpected
Updating 1a2b3c4..5d6e7f8 Fast-forward file.txt | 2 ++ 1 file changed, 2 insertions(+)
View the commit history graph to see the difference between rebased and merged commits.
Terminal
git log --oneline --graph --all
Expected OutputExpected
* 5d6e7f8 (HEAD -> main) Add new feature * 1a2b3c4 Previous commit on main
Key Concept

If you remember nothing else, remember: rebase rewrites history to create a straight line of commits, while merge preserves the branch history with a merge commit.

Common Mistakes
Using rebase on a branch that others are also using
Rebasing rewrites history, which can confuse others and cause conflicts when they try to update their branches.
Use rebase only on local or private branches before sharing, and use merge for shared branches.
Merging without updating the feature branch first
This can create unnecessary conflicts or merge commits that could have been avoided.
Update your feature branch with rebase or merge from main before merging it back.
Summary
Use 'git rebase main' on your feature branch to apply your changes on top of the latest main branch commits, creating a linear history.
Use 'git merge feature-branch' on main to combine branches and preserve the full branch history with a merge commit.
Rebase rewrites commit history and should be used carefully, especially on shared branches.

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