Skip to main content
junior
beginner
Git

Git Rebase vs Merge

Question

What is the difference between git rebase and git merge? When would you use each?

Answer

Merge creates a new commit combining two branches, preserving history. Rebase rewrites history by moving commits to a new base, creating a linear history. Use merge for shared branches to preserve context; use rebase for feature branches to keep history clean. Never rebase commits that have been pushed to shared branches.

Why This Matters

Both achieve branch integration but with different history outcomes. Merge is safer and preserves the complete development timeline. Rebase creates cleaner history but rewrites commits, which can cause issues if others have based work on those commits.

Code Examples

Merge workflow

bash

Rebase workflow

bash
Common Mistakes
  • Rebasing commits that have already been pushed to a shared branch
  • Force pushing after rebase without communicating with team members
  • Using rebase when merge commits provide valuable context
Follow-up Questions
Interviewers often ask these as follow-up questions
  • What is interactive rebase and when would you use it?
  • How do you resolve conflicts during a rebase?
  • What is the golden rule of rebasing?
Tags
git
version-control
branching
workflow