What is a Rebase
A rebase moves a sequence of commits from one base to another. It replays each commit's diff onto the new base, creating new commit objects. The result is linear history — the rebased commits appear to have been written on top of the new base.
How it works
git rebase main while on a feature branch:
- Finds the merge base between
featureandmain. - Computes the diff for each commit on
featuresince the merge base. - Resets
featureto the tip ofmain. - Applies each diff as a new commit, one at a time.
Each replayed commit gets a new SHA-1 hash because its parent has changed. The old commits become unreachable (recoverable via reflog).
Interactive rebase (git rebase -i) lets you squash, reorder, edit, or drop commits before replaying them. This is the standard way to clean up a feature branch before merging.
Why it matters
Rebase produces clean, linear history that is easy to read and bisect. But it rewrites history — never rebase commits that others have already fetched. The golden rule: rebase local work before sharing, merge shared work after fetching.
See How Git Rebase Works for interactive rebase, conflict handling, and the golden rule.