All chapters in this track
- 011. The Mental Model
- 022. Essential Setup
- 033. Core Daily Workflow
- 044. Branching
- 055. Merging & Conflict Resolution
- 066. Rebasing
- 077. Remote Repositories
- 088. Stashing
- 099. Undoing Things
- 1010. Tagging & Versioning
- 1111. Cherry-Pick
- 1212. Git Bisect & Blame
- 1313. Git Hooks
- 1414. Submodules
- 1515. Team Workflows
- 1616. Power Commands & Aliases
- 1717. Reflog: The Safety Net
- 1818. Practice Tasks
- 19Quick Reference Cheatsheet
Chapter 6 of 19
6. Rebasing
Rebase replays your commits on top of another branch. Result: linear history.
# Basic rebase
git switch feature/login
git rebase main # replay feature commits on top of main
# After rebase, go back to main and fast-forward merge
git switch main
git merge feature/login # fast-forward, no merge commit
Interactive rebase (clean up history)
git rebase -i HEAD~4 # edit last 4 commits
Editor opens with a list of commits. Change the action keyword:
| Action | Effect |
|---|---|
pick | keep as-is |
reword | change commit message |
edit | pause to amend |
squash | meld into previous commit, combine messages |
fixup | meld, discard this commit's message |
drop | delete this commit entirely |
Example: squash 3 WIP commits into one before merge
pick a1b2c3 feat: scaffold login form
squash d4e5f6 wip: add validation
squash g7h8i9 fix: edge case null email
Result: one clean commit with a combined message.
Golden rule
Never rebase commits that have been pushed to a shared branch.
Rebase rewrites history. If others have pulled those commits, everyone's history diverges.
Safe rebase uses:
- Local-only branches
- Your own remote branch (before others pull it)
- Feature branches not yet merged