Skip to main content
Git

Git interactive rebase: squash, reorder, and rewrite commits before they hit main

Interactive rebase is the cleanest way to turn exploratory branch history into a reviewable story—provided the branch is yours and nobody else is building on it.

Thien Nguyen
By Thien Nguyen
Updated July 21, 2026 · 2 min read

Feature work is usually messy: a spike, a fix, a formatting commit, a test correction. That is normal. Before review, interactive rebase lets you present the history as a sequence a reviewer can understand and bisect.

git rebase -i <base> opens a todo list where pick, reword, squash, fixup, and reordering let you edit commits that exist only on your branch. The one rule: never rewrite a shared branch without coordinating, because rewritten commits are new commits with new identities.

pick   1a2b add parser
fixup  3c4d typo in parser
pick   5e6f add tests
reword 7a8b explain error handling
CommandUse it when
rewordCode is right; commit message is not
fixupFold a correction into the previous commit silently
squashCombine commits and edit the message
editPause to amend code or split a commit
dropRemove obsolete work

A safe branch cleanup

git fetch origin
git rebase -i origin/main
git push --force-with-lease

--force-with-lease protects you from overwriting a remote update you did not have locally. Plain --force does not. If conflicts appear, resolve them, run the relevant tests, git add, then git rebase --continue.

Rebase does not erase the old remote history from other clones. It changes the branch tip. Tell collaborators before you force-push a branch they may have checked out.

Good history is not cosmetic: it makes review narrower, reverts safer, and git bisect more useful. Clean it while the intent is still in your head.

Split a mixed commit instead of hiding it

Choose edit in the todo list, reset the commit while retaining its changes, then stage logical pieces:

git reset HEAD^
git add -p
git commit -m "Add parser"
git add -p
git commit -m "Add parser tests"
git rebase --continue

This is most valuable before review, when a formatting sweep, refactor, and behavioural change accidentally landed together. Keep commits buildable where practical; it lets a reviewer and git bisect move through history without reconstructing your working state.

Cover photo by luis gomes on Pexels.

References

Primary documentation and specifications checked when this article was last updated.

GitWorkflowDeveloper Tools

Related articles

All articles