Git & Version Controladvanced
Guide through interactive rebase with explanations
Interactive Rebase
Guide through interactive rebase with explanations
Guide through interactive rebase to clean up commit history.
Instructions
- Start interactive rebase for the last N commits:
git rebase -i HEAD~5 # last 5 commits
# or
git rebase -i main # all commits since branching from main
- Explain rebase actions:
pick — keep the commit as-is
reword — keep the commit but change its message
edit — stop and let you amend the commit
squash — combine with previous commit, merge messages
fixup — combine with previous commit, discard this message
drop — remove the commit entirely
- Common rebase recipes:
Squash all branch commits into one:
Change all but first pick to squash (or fixup)
Reorder commits: Just move lines up/down in the editor
Split a commit:
Change to edit, then:
git reset HEAD~1
git add file1 && git commit -m "part 1"
git add file2 && git commit -m "part 2"
git rebase --continue
- If things go wrong:
git rebase --abort # cancel and go back to original state
Rules
- Never rebase commits that are already pushed to a shared branch
- If rebasing a pushed branch, you'll need
git push --force-with-lease(safer than--force)