Interactive Rebase & History Rewriting
Beginnerv1.0.0
Clean up messy commit history with interactive rebase — squash, reorder, edit, and split commits to create a polished, reviewable Git history.
Content
Overview
Interactive rebase (git rebase -i) lets you rewrite commit history before sharing it. Squash WIP commits, reorder for logical flow, edit messages, and split oversized commits into focused units.
When to Use
- -Before creating a PR — clean up your local commits
- -After code review feedback — reorganize commits logically
- -To combine "fix typo" and "WIP" commits into meaningful units
When NOT to Use
- -On commits already pushed to shared branches (main, develop)
- -On public/open-source branches others have forked
- -If you're not comfortable with conflict resolution
Basic Usage
The Interactive Menu
Available Commands
| Command | Short | What It Does |
|---|---|---|
| `pick` | `p` | Keep commit as-is |
| `reword` | `r` | Keep commit, edit message |
| `edit` | `e` | Pause to amend commit |
| `squash` | `s` | Merge into previous commit, combine messages |
| `fixup` | `f` | Merge into previous, discard this message |
| `drop` | `d` | Remove commit entirely |
Common Workflows
Squash WIP Commits
Result: 2 clean commits instead of 5 messy ones.
Reorder Commits for Logical Flow
Split a Large Commit
Safety Tips
- -Always create a backup branch before rebasing:
git branch backup-before-rebase - -If things go wrong:
git rebase --abortreturns to original state - -Use
git reflogto recover lost commits after a bad rebase - -Never rebase commits that have been pushed to shared branches
Best Practices
- -Rebase BEFORE pushing, never after others have pulled
- -Aim for each commit to be independently buildable and testable
- -Use
fixup(notsquash) when the intermediate message has no value - -Group related changes together in the final history
FAQ
Discussion
Loading comments...