Interactive Rebase
Interactive Rebase: Squash, Reorder, Fixup, Polish
After this page you can rewrite a short chain of local commits on Linux without a GUI.
Open the todo list
cd ~/desk/desk-api
git switch feat/ping-json
git rebase -i HEAD~3Git writes a file and opens core.editor (nvim in this book). Each line is a commit, oldest first:
pick a1b2c3d feat(ping): return a dict
pick d4e5f6a docs: mention JSON
pick 98ab765 fix typo
You edit the verbs, save, quit. Git replays from the top.
Verbs you will use
| Verb | Effect |
|---|---|
pick |
Keep this commit |
reword |
Keep, edit the message |
edit |
Stop here so you can amend |
squash |
Fold into the previous commit; editor for a combined message |
fixup |
Fold into the previous commit; keep the previous message |
drop |
Omit this commit |
Reorder lines to reorder commits. Delete a line to drop (same as drop).
Linux without the editor (labs)
For a scripted squash of everything after the first of the last three:
GIT_SEQUENCE_EDITOR="sed -i '2,\$s/^pick /fixup /'" git rebase -i HEAD~3sed -i on GNU sed (Linux) edits in place. This is for labs and hooks. Day to day, use nvim.
Stop and amend (edit)
Git stops at that commit. You git add / git commit --amend, then:
git rebase --continueAbort
git rebase --abortBack to the pre-rebase branch tip. Safe while the rebase is in progress.
Range
git rebase -i main rebases each commit reachable from HEAD but not from main. That is the usual “update my feature onto main” todo list.
Do this on a feature branch. Interactive rebase of shared main rewrites hashes everyone already has.