Bird
Raised Fist0
Gitdevops~30 mins

When to rebase vs when to merge in Git - Hands-On 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
When to Rebase vs When to Merge in Git
📖 Scenario: You are working on a team project using Git. You have a main branch called main and a feature branch called feature. You want to learn how to keep your feature branch up to date with the latest changes from main using either git rebase or git merge. This will help you avoid conflicts and keep your project history clean.
🎯 Goal: Learn when to use git rebase and when to use git merge to update your feature branch with changes from main. Practice commands to rebase and merge, and see the difference in commit history.
📋 What You'll Learn
Use git checkout to switch branches
Use git merge to merge branches
Use git rebase to rebase branches
Understand the difference in commit history after merge vs rebase
💡 Why This Matters
🌍 Real World
Teams use Git to collaborate on code. Knowing when to merge or rebase helps keep the project history clean and understandable.
💼 Career
Software developers and DevOps engineers often need to update feature branches with main branch changes. Understanding merge vs rebase is essential for smooth teamwork and code integration.
Progress0 / 4 steps
1
Setup branches and initial commits
Create a new Git repository. Then create a file called file.txt with the text Initial content. Commit this change on the main branch. Next, create and switch to a branch called feature. On feature, change file.txt to have the text Feature work and commit this change.
Git
Hint

Use git init to start the repo. Use git checkout -b feature to create and switch to the feature branch.

2
Add a commit on main branch
Switch back to the main branch. Create a new file called main.txt with the text Main branch update. Commit this change on main.
Git
Hint

Use git checkout main to switch back. Then edit and commit the file.

3
Update feature branch using git merge
Switch to the feature branch. Use git merge main to bring the changes from main into feature. This will create a merge commit.
Git
Hint

Use git checkout feature to switch. Then run git merge main.

4
Update feature branch using git rebase
Reset the feature branch to before the merge. Then switch to feature branch again. Use git rebase main to replay your feature commits on top of main. Finally, print the commit log with git log --oneline --graph --all to see the difference in history.
Git
Hint

Use git reset --hard HEAD~1 to undo the merge commit. Then git rebase main. Finally, run git log --oneline --graph --all to see the commit history.

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