Use merge on shared branches. Use rebase to tidy your own branch before others pull it.
git merge records the join in a new commit and rewrites nothing, so merge is safe everywhere. The cost is a busier history, because every merge of diverged branches adds a merge commit.
git rebase replays your commits onto the other branch. The copies get new hashes, so they are new commits. Anyone who already had the old ones now holds a diverged branch, which is why you never rebase shared history.
| Concern | git merge |
git rebase |
|---|---|---|
| History | branch and join | one straight line |
| Existing commits | kept as they are | replaced by copies with new hashes |
| Conflicts | fixed once | can repeat per replayed commit |
A common workflow uses both. Rebase the feature branch onto the newest main, then merge it:
git switch feature
git rebase main # replay my commits on the newest main
git switch main
git merge feature # publish the finished work
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
The diagram below the answer is the concept . Jump to it ↓