Bird
Raised Fist0
Gitdevops~5 mins

When to rebase vs when to merge 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. Rebasing and merging are two ways to do this. Choosing the right one helps keep your project history clean and easy to understand.
When you want to update your feature branch with the latest changes from the main branch before finishing your work.
When you want to keep a simple, straight history without extra merge commits.
When you want to combine completed work from a feature branch back into the main branch with a clear record of merges.
When collaborating with others and you want to avoid rewriting shared history.
When you want to resolve conflicts early by replaying your changes on top of the latest code.
Commands
Switch to your feature branch where you want to apply changes.
Terminal
git checkout feature-branch
Expected OutputExpected
Switched to branch 'feature-branch'
Rebase your feature branch on top of the latest main branch commits to keep history linear.
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.
Terminal
git checkout main
Expected OutputExpected
Switched to branch 'main'
Merge the feature branch into main, creating a merge commit that records the integration.
Terminal
git merge feature-branch
Expected OutputExpected
Updating 1a2b3c4..5d6e7f8 Fast-forward file.txt | 2 ++ 1 file changed, 2 insertions(+)
Key Concept

If you remember nothing else, remember: rebase rewrites history to keep it clean, merge preserves history and shows integration points.

Common Mistakes
Rebasing a branch that others are also using.
It rewrites history, causing conflicts and confusion for others sharing the branch.
Only rebase local or private branches that are not shared with others.
Merging too often without rebasing, leading to many merge commits.
This clutters the project history and makes it harder to follow changes.
Use rebase to keep your branch up to date before merging to main.
Using rebase to combine unrelated branches or public branches.
This can cause complex conflicts and disrupt collaboration.
Use merge for combining public branches or when you want to preserve the full history.
Summary
Use git rebase to update your feature branch with the latest main branch changes and keep history linear.
Use git merge to combine branches and preserve the full history with merge commits.
Avoid rebasing shared branches to prevent rewriting history that others rely on.

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