Rewriting History
Rebase rewrites commit history to create a cleaner, more linear timeline. Powerful but requires caution.
Basic Rebase
# From feature branch
git rebase mainThis replays your commits on top of main, as if you started your feature from the latest main.
Rebase vs Merge
- Merge: Preserves history, creates merge commits
- Rebase: Creates linear history, rewrites commits
Interactive Rebase
git rebase -i HEAD~3Edit last 3 commits. Options:
pick: Use commitreword: Change messageedit: Modify commitsquash: Combine with previousdrop: Remove commit
Squashing Commits
Combine multiple commits into one:
git rebase -i HEAD~3Change to:
pick abc123 First commit
squash def456 Second commit
squash ghi789 Third commit
Resolving Rebase Conflicts
- Fix conflicts in files
git addresolved filesgit rebase --continue
Aborting Rebase
git rebase --abortGolden Rule
Never Rebase Public Commits
Don't rebase commits that have been pushed to shared branches. This rewrites history others depend on, causing chaos.
Rebase is safe for:
- Local commits not yet pushed
- Personal feature branches
- Cleaning up before creating PR
When to Use Rebase
- Clean up messy local commits
- Keep feature branch updated with main
- Create linear history for easier reading
When to Use Merge
- Shared/public branches
- Want to preserve context
- Team prefers merge commits
Next: Vessel Switching
Rebase is powerful but dangerous. Next, learn git switch—the modern way to change branches.