Git merge vs rebase comes down to history shape: git merge joins two branches with a merge commit and keeps the history exactly as it happened, while git rebase replays your commits on top of another branch to produce a straight line. Use merge for shared branches and for integrating finished work, and use rebase to tidy a private feature branch before you share it.
Both commands solve the same problem: your branch and its base have diverged, and you need to combine the work. They differ in what they record and in what they rewrite.
A merge takes the tips of two branches, finds their common ancestor, and creates a new commit with two parents that contains the combined result. Nothing that already exists is changed.
If main has not moved since you branched, Git only moves the main pointer forward (a fast-forward) and creates no merge commit. If both branches have new commits, you get a merge commit:
Because existing commits keep their IDs, merge is safe on branches other people have already pulled. The cost is a history with many branches and merge commits, which can be harder to read.
A rebase takes the commits that exist only on your branch, sets them aside, moves your branch to the tip of the target branch, and applies your commits again one by one.
The history is now linear. The important detail is that the replayed commits are new commits. They have the same messages and changes, but different parents, so they get different IDs. The old commits still exist for a while in the reflog, but your branch no longer points to them.
| Aspect | Merge | Rebase |
|---|---|---|
| History shape | Branching, with merge commits | Linear |
| Existing commits | Unchanged | Replaced by new commits with new IDs |
| Conflicts | Resolved once, in the merge commit | May be resolved once per replayed commit |
| Safe on shared branches | Yes | No, it needs a force push afterwards |
| Records when work was integrated | Yes | No |
| Easy to undo | git revert -m 1 <merge> or reset | git reflog then git reset |
Choose merge when you want an honest record of how work came together.
main, especially through a pull request.Use git merge --no-ff feature/login to force a merge commit even when a fast-forward is possible, so the feature stays visible as a group of commits.
Choose rebase when you want a clean, readable history and the commits are yours alone.
main before opening a pull request, instead of merging main into it repeatedly.git rebase -i, for example squashing "fix typo" commits.The last line is needed because rebasing changed your commits. --force-with-lease refuses to overwrite the remote branch if someone else pushed to it since you last fetched, which makes it much safer than plain --force.
Most teams combine the two commands rather than picking one forever.
main and commit freely.git fetch and git rebase origin/main to stay current. Nobody else depends on your branch yet.main using the strategy your team chose: merge commit, squash merge or rebase merge.If a rebase goes wrong, run git rebase --abort to return to the state before it started.
Neither is better in general. Rebase gives a cleaner history but rewrites commits, so it is only safe on commits nobody else has. Merge is always safe but produces a more complex graph.
It replaces them with new copies. The originals stay reachable through git reflog for a period, so you can recover them with git reset --hard to an earlier reflog entry.
The rebased commits have new IDs, so your local branch and the remote branch no longer share the same history. Git rejects a normal push, and you must use git push --force-with-lease.
You can choose with git pull --rebase or git pull --no-rebase, or set pull.rebase in your configuration. Rebasing on pull avoids needless merge commits for your local work.
Merge preserves history and never rewrites commits, which makes it the safe choice for shared branches. Rebase rewrites your commits onto a new base to give a linear history, which suits private branches before review. Rebase only commits that have not been shared, push with --force-with-lease, and use git rebase --abort if you need to back out.