Interactive Rebase
Git History Cleanup: The Interactive Rebase
Use this process to delete “debug” commits, fix typos in messages, or combine multiple small commits into one clean feature commit.
1. Setup (One-time)
Ensure VS Code is your default editor so you don’t get stuck in the terminal (Vim).
# Set VS Code as the global editor
git config --global core.editor "code --wait"Note: If “code” command is not found, use VS Code Command Palette (Cmd+Shift+P) and run “Install ‘code’ command in PATH”.
2. The Cleanup Workflow
Step 1: Target the History
Determine how many commits back you need to go (e.g., the last 5).
git rebase -i HEAD~5Step 2: Edit the Instruction File
VS Code will open a list of commits. Order: Oldest is at the top.
| Command | Short | What it does |
|---|---|---|
| pick | p | Keeps the commit exactly as it is. |
| fixup | f | Merges the code into the commit above it and deletes the message. |
| reword | r | Keeps the code but lets you rename the commit message. |
| drop | d | Deletes the commit and the code changes entirely. |
Example of a “Debug Cleanup”:
pick a1b2c3d Initial feature implementation
f e5f6g7h debug: check variable x <-- Merges into 'Initial feature'
f i9j0k1l PLEASE WORK <-- Merges into 'Initial feature'
pick m2n3o4p Final clean bug fixAction: Save the file and close the tab.
3. The “Force” Sync
Because you changed the history, GitHub will reject a normal push. You must overwrite the remote branch.
# ONLY do this on your own branches, never on a shared main branch!
git push origin your-branch-name --force4. Emergency Escape Hatch
If things go wrong during the rebase (conflicts or accidental deletions):
- To Abort: git rebase –abort (Returns everything to exactly how it was).
- To Undo a Push: Use git reflog to find your previous state (this is for advanced recovery).