The Core Difference
Both git merge and git rebase integrate changes from one branch into another — but they produce different history shapes. Merge preserves history as it happened (with a merge commit). Rebase rewrites history to look like changes happened in sequence (no merge commit, cleaner log).
Git Merge
# Merge feature branch into main
git checkout main
git merge feature/my-feature
# Fast-forward merge (no merge commit if possible)
git merge --ff-only feature/my-feature
# Always create a merge commit
git merge --no-ff feature/my-feature
Result: A merge commit appears in history that ties together both branches. History shows exactly when branches diverged and when they merged.
Git Rebase
# Rebase feature branch onto main
git checkout feature/my-feature
git rebase main
# Resolve conflicts if they appear, then:
git rebase --continue
# Or abort if things go wrong
git rebase --abort
# Interactive rebase — squash, reorder, edit commits
git rebase -i HEAD~5
Result: Feature branch commits are replayed on top of main. History looks linear — as if development happened sequentially.
When to Use Each
| Scenario | Use |
|---|---|
| Merging a feature branch to main | Merge (preserves context) |
| Updating your feature branch with latest main | Rebase (cleaner, no merge commits) |
| Cleaning up messy local commits before PR | Interactive rebase |
| Public/shared branches | Merge only (never rebase shared branches) |
The Golden Rule of Rebase
Never rebase commits that have been pushed to a shared repository. Rebase rewrites commit hashes. If others have based work on those commits, their history diverges and they'll have conflicts that are painful to resolve.
Interactive Rebase — Clean Up Before Pushing
# Squash last 3 commits into one
git rebase -i HEAD~3
# In the editor, change 'pick' to 's' (squash) for commits to merge:
# pick abc1234 First commit
# s def5678 Fix typo
# s ghi9012 Another fix
Frequently Asked Questions
Does rebase rewrite history?
Yes — this is the key point. Rebase creates new commits with new SHA hashes. The content changes are the same but the commits are technically different objects in Git's object store.
What does git pull --rebase do?
Instead of creating a merge commit when pulling remote changes, it rebases your local commits on top of the fetched remote commits. Produces cleaner history on feature branches.