Tutorial — mastering interactive rebase
git bisect effective. Interactive rebase ( git rebase -i ) is the tool to rewrite a branch before pushing it — squash, rename, reorder, drop commits. Here's how to wield it without getting burned.
Opening the todo
git rebase -i takes a base : every commit that comes after it becomes editable. You usually target the last N commits of the current branch.
The everyday commands
- ›
reword(r): keep the commit but rewrite its message. - ›
squash(s): merge into the previous commit while keeping both messages. - ›
fixup(f): like squash, but discard the merged commit's message — perfect for a
- ›
edit(e): stop on the commit to amend the code or split it. - ›
drop(d): remove the commit entirely.
git add and git rebase --continue . At any point, git rebase --abort brings the branch back to its pre-rebase state.
Automatic fixup
--fixup then --autosquash order everything for you:
The golden rule
git push --force will diverge from their copy and cause nasty conflicts. So you rebase only a local branch not yet pushed — or a branch you own alone, with a git push --force-with-lease that refuses to crush unexpected work.
Recovering after a mistake
git reflog keeps a trace of every position of HEAD , even the ones "lost" by the rewrite.
Interactive rebase rewrites history to make it tellable : one commit = one idea, one clear message. Keep it local, secure your pushes with --force-with-lease, and remember the reflog is your safety net.