/compare/git/git-merge-vs-git-rebase
git merge vs git rebase: differences and when to use each
verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash) · git version 2.55.0.windows.5updated 2026-10-20
- tool
- Git
- compares
- git merge vs git rebase
git merge combines two branches by creating a merge commit; existing commits stay untouched. git rebase replays your branch's commits on top of another branch, producing a straight line of new commits with new hashes. Merge is safe for shared branches; rebase gives a cleaner history but must not be used on commits others have already pulled.
Same branches, two results
git init -q -b main repo && cd repo
echo base > f && git add f && git commit -qm "base"
git switch -q -c feature && echo feat > g && git add g && git commit -qm "feature work"
git switch -q main && echo fix > h && git add h && git commit -qm "fix on main"
git branch -q feature-copy feature
echo "--- merge:"
git merge -q --no-edit feature && git log --oneline --graph
git reset -q --hard HEAD~1
echo "--- rebase:"
git switch -q feature-copy && git rebase -q main && git log --oneline --graphOutput
--- merge: * da13255 Merge branch 'feature' |\ | * d2e54c4 feature work * | 249e3b5 fix on main |/ * 350f8df base --- rebase: * 4f9e7f7 feature work * 249e3b5 fix on main * 350f8df base
Criterion by criterion
| Criterion | git merge | git rebase |
|---|---|---|
| History shape | branching, with merge commits | linear |
| Existing commits | unchanged | replaced by new commits (new hashes) |
| Conflicts | resolved once, in the merge commit | resolved commit by commit |
| Safe on shared branches | yes | no: needs a force push and breaks others' clones |
| Shows when work was integrated | yes (merge commit) | no |
| Typical use | integrating feature branches into main | updating your own branch before a pull request |
Rule of thumb
Rebase your local, unpublished work onto the latest main (git pull --rebase, git rebase main) to keep history tidy. Merge when integrating a branch that other people use, or when you want the merge to be visible in history.
Sources
Related pages
errors · giterror: failed to push some refs (rejected, fetch first)git push is rejected with failed to push some refs when the remote branch has commits you do not have. Pull or fetch and rebase first, then push again safely.flags · gitgit push --force-with-lease: a safer force push--force-with-lease overwrites the remote branch only if it still points where your remote-tracking ref says, so commits pushed since your fetch are never lost.cheat sheets · gitGit cheat sheet: everyday commandsGit cheat sheet: setup, stage and commit, branches with switch, history, undo with restore, reset and revert, stash, remotes, tags and the reflog on one page.