Git Merge vs Rebase — Which One Should You Use?
git merge and git rebase both integrate changes from one branch into another — but they work differently.
The Difference
Merge creates a new commit that ties two branch histories together.
Rebase rewrites history by replaying your commits on top of another branch.
# Merge preserves history
A---B---C main
\
D---E feature
\
A---B---C------F main (merge commit)
# Rebase rewrites history
A---B---C main
\
D'---E' feature (new commits)When to Merge
Merging to main (shared branch)
git checkout main
git merge featureUse merge when integrating a feature branch into a shared branch like main or develop. The merge commit makes it clear when the feature was integrated.
Public branches
Never rebase branches that others are working on. Use merge for any branch that has been pushed and shared.
When to Rebase
Cleaning up local history
git checkout feature
git rebase mainBefore pushing a feature branch, rebase it onto the latest main. This keeps history linear and avoids unnecessary merge commits.
Interactive rebase
git rebase -i HEAD~5Squash, reorder, and reword commits before pushing. Never do this after pushing.
The Golden Rule
Never rebase a branch that others have based work on.
If a branch has been pushed and shared, use merge. Rebasing a shared branch creates duplicate commits and breaks everyone else’s history.
Example Workflow
# Start a feature
git checkout -b my-feature main
# Work, commit, work, commit
git add .
git commit -m "Add feature part 1"
git add .
git commit -m "Add feature part 2"
# Update with latest main (rebase locally)
git rebase main
# Clean up commits
git rebase -i HEAD~2
# Push (first time)
git push -u origin my-feature
# Open PR, get review, make changes
git add .
git commit -m "Fix review feedback"
git rebase -i HEAD~3 # squash fixup into proper commits
# Force push (safe here — it's your feature branch)
git push --force-with-lease
# Merge PR (use merge, not rebase, on main)
# GitHub: "Create a merge commit"Resolving Conflicts
During merge
git merge feature
# CONFLICT in file.txt
# Fix conflicts
git add file.txt
git merge --continueDuring rebase
git rebase main
# CONFLICT in file.txt
# Fix conflicts
git add file.txt
git rebase --continue
# Or skip this commit
git rebase --skip
# Or abort entirely
git rebase --abortPR Workflow Comparison
| Action | Merge | Rebase |
|---|---|---|
| Update feature branch | git merge main | git rebase main |
| History on main | Linear + merge commits | Linear only |
| Commit authorship | Preserved | Preserved (new hashes) |
| Safe for shared branches | Yes | No |
Quick Decision Guide
| Situation | Use |
|---|---|
| Integrating a PR into main | Merge |
| Updating your local feature branch | Rebase |
| Shared branch with collaborators | Merge |
| Cleaning commits before push | Rebase (interactive) |
| Branch that has been pushed | Merge |
| Branch you haven’t pushed yet | Rebase |
Related: Learn Git branching strategies and advanced Git commands.