Rebase - a Cleaner History
git rebase
Rebase replays your commits on top of another branch, producing a straight, linear history instead of a merge commit.
Before: A─B─C (main)
\
D─E (feature)
After rebase onto main: A─B─C─D'─E' (feature) ← linear, no merge commit
$ git switch feature
$ git rebase main
| Merge | Rebase |
|---|---|
| Preserves exact history + merge commit | Rewrites commits for a clean line |
| Safe on shared branches | Only for local/unpushed commits |
Danger: The golden rule of rebase: never rebase commits you've already pushed and shared. Rebasing rewrites commit IDs; if others based work on the originals, you've split history. Rebase local work; merge shared work.
Tip: Interactive rebase (git rebase -i HEAD~3) lets you squash, reorder, reword, or drop recent commits - great for tidying a messy branch before opening a PR.