Bird
Raised Fist0
Gitdevops~10 mins

When to rebase vs when to merge 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 - When to rebase vs when to merge
Start: Branches diverge
Choose action
Rebase
Linear history
Clean history
End: Updated branch
This flow shows the choice between rebasing and merging when branches diverge, leading to either a linear history with rebase or a merge commit preserving context.
Execution Sample
Git
git checkout feature
# Option 1: Rebase
 git rebase main

# Option 2: Merge
 git merge main
This code shows switching to a feature branch and then either rebasing it onto main or merging main into it.
Process Table
StepActionBranch State BeforeCommandBranch State AfterResulting History
1Start on feature branch diverged from mainfeature and main have different commitsgit checkout featureOn feature branchBranches diverged
2Rebase feature onto mainfeature behind maingit rebase mainfeature commits replayed on top of mainLinear history, no merge commit
3Merge main into featurefeature behind maingit merge mainfeature branch has merge commitHistory shows merge commit preserving context
4Endfeature updatednonefeature updated with main changesEither linear or merge commit history
💡 Execution stops after feature branch is updated either by rebase or merge.
Status Tracker
VariableStartAfter Step 2 (Rebase)After Step 3 (Merge)
feature branch commitsA-B (diverged from main)A-B replayed on top of main (linear)A-B plus merge commit with main
Key Moments - 3 Insights
Why does rebase create a linear history?
Because rebase moves feature branch commits to the tip of main, replaying them as new commits, as shown in execution_table step 2.
Why does merge create a merge commit?
Merge combines histories preserving the original commits and adds a merge commit, as shown in execution_table step 3.
When should I prefer rebase over merge?
Prefer rebase for a clean, linear history when working alone or before sharing; prefer merge to preserve context when collaborating, as implied by the branch states in variable_tracker.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the resulting history after running 'git rebase main' on feature branch?
AFeature branch is deleted
BLinear history with no merge commit
CHistory with a merge commit
DMain branch is reset
💡 Hint
See execution_table row 2 under 'Resulting History'
At which step does the feature branch get a merge commit?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Check execution_table row 3 under 'Branch State After'
If you want to keep a clean history without merge commits, which command should you use?
Agit checkout main
Bgit merge main
Cgit rebase main
Dgit reset --hard
💡 Hint
Refer to variable_tracker and execution_table step 2 for linear history
Concept Snapshot
When branches diverge, use 'git rebase' to replay commits on main for a clean linear history.
Use 'git merge' to combine branches preserving all commits with a merge commit.
Rebase rewrites history; merge preserves it.
Rebase is good before sharing; merge is safer for collaboration.
Choose based on whether you want clean history or context preservation.
Full Transcript
When working with Git branches, you often need to update your feature branch with changes from the main branch. You can do this by either rebasing or merging. Rebasing moves your feature commits on top of the latest main branch commits, creating a straight, linear history without merge commits. Merging combines the histories and adds a merge commit, preserving the context of how branches came together. Use rebase when you want a clean history and are working alone or before sharing your branch. Use merge when collaborating to keep the full history intact. The execution table shows the step-by-step changes in branch state and history after each command.

Practice

(1/5)
1. What is the main reason to use git rebase instead of git merge?
easy
A. To delete the feature branch after merging
B. To keep all branch merge points visible in history
C. To create a clean, linear history without merge commits
D. To automatically resolve all conflicts

Solution

  1. Step 1: Understand rebase purpose

    Rebase moves commits to create a straight line history without merge commits.
  2. Step 2: Compare with merge

    Merge keeps all branch points and creates merge commits, showing full history.
  3. Final Answer:

    To create a clean, linear history without merge commits -> Option C
  4. Quick Check:

    Rebase = linear history [OK]
Hint: Rebase = linear history, Merge = full branch history [OK]
Common Mistakes:
  • Thinking merge creates linear history
  • Believing rebase deletes branches
  • Assuming rebase auto-resolves conflicts
2. Which of the following is the correct syntax to rebase your current branch onto main?
easy
A. git rebase origin/main
B. git merge main
C. git checkout main && git rebase
D. git rebase main

Solution

  1. Step 1: Identify rebase command

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

    git merge main merges, not rebases; git checkout main && git rebase is incomplete; git rebase origin/main rebases onto remote branch, not local main.
  3. Final Answer:

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

    Rebase syntax = git rebase branch [OK]
Hint: Rebase current branch onto main: git rebase main [OK]
Common Mistakes:
  • Confusing merge and rebase commands
  • Using incomplete rebase syntax
  • Rebasing onto remote branch unintentionally
3. You have a feature branch with 3 commits diverged from main. After running git rebase main, what will the commit history look like?
medium
A. The 3 commits will be replayed on top of the latest main commits
B. The 3 commits will be merged into a single commit on main
C. The 3 commits will be deleted and replaced by main commits
D. The 3 commits will remain unchanged and main will be merged

Solution

  1. Step 1: Understand rebase effect on commits

    Rebase takes your commits and re-applies them on top of the target branch, here main.
  2. Step 2: Compare with merge behavior

    Merge combines histories with a merge commit; rebase rewrites history to appear linear.
  3. Final Answer:

    The 3 commits will be replayed on top of the latest main commits -> Option A
  4. Quick Check:

    Rebase = replay commits on new base [OK]
Hint: Rebase replays commits on top of target branch [OK]
Common Mistakes:
  • Thinking rebase merges commits
  • Assuming commits are deleted
  • Confusing merge and rebase results
4. You tried to rebase your branch onto main but got conflicts. What is the correct way to continue after resolving conflicts?
medium
A. Run git rebase --continue after fixing conflicts
B. Run git merge --continue after fixing conflicts
C. Run git commit to finish the rebase
D. Run git rebase --abort to keep the changes

Solution

  1. Step 1: Identify rebase conflict resolution

    After fixing conflicts during rebase, you must run git rebase --continue to proceed.
  2. Step 2: Check other options

    git merge --continue is for merge conflicts; git commit alone doesn't continue rebase; git rebase --abort cancels rebase.
  3. Final Answer:

    Run git rebase --continue after fixing conflicts -> Option A
  4. Quick Check:

    Fix conflicts + git rebase --continue [OK]
Hint: After conflict fix in rebase, run git rebase --continue [OK]
Common Mistakes:
  • Using git merge --continue during rebase
  • Running git commit instead of rebase continue
  • Aborting rebase instead of continuing
5. Your team wants to keep a clear record of all branch merges for auditing, but also wants to avoid complex conflict resolution during integration. Which strategy should you choose?
hard
A. Use git rebase to keep history linear and avoid merge commits
B. Use git merge to preserve branch history and avoid rewriting commits
C. Use git rebase and then merge to keep both histories
D. Use git cherry-pick to manually apply commits

Solution

  1. Step 1: Understand audit needs

    Keeping a clear record means preserving all branch merge points and history.
  2. Step 2: Compare merge and rebase for conflicts

    Merge preserves history and avoids rewriting commits, reducing conflict complexity; rebase rewrites history and can cause conflicts.
  3. Final Answer:

    Use git merge to preserve branch history and avoid rewriting commits -> Option B
  4. Quick Check:

    Audit needs = merge to keep history [OK]
Hint: Preserve history and audit: choose git merge [OK]
Common Mistakes:
  • Choosing rebase when audit needs full history
  • Mixing rebase and merge without clear purpose
  • Using cherry-pick for full branch integration