Interactive rebase lets you rewrite history: reorder, squash, edit, or drop commits before sharing them.
git rebase -i HEAD~4Your editor opens with:
pick a1b2c3d Add user model
pick b2c3d4e Add user controller
pick c3d4e5f Fix typo in user model
pick d4e5f6g Add user routes
pick = use commit as-is
reword = use commit, but edit the message
edit = use commit, but stop to amend it
squash = meld into previous commit (keep message)
fixup = meld into previous commit (discard message)
drop = remove commit entirely
Change the file to:
pick a1b2c3d Add user model
fixup c3d4e5f Fix typo in user model
pick b2c3d4e Add user controller
pick d4e5f6g Add user routes
🧠 What happened? We moved the typo fix right after the original commit and changed pick to fixup. Git will combine them into one commit and discard the "Fix typo" message.
git log --onelineg7h8i9j Add user routes
f6g7h8i Add user controller
e5f6g7h Add user model
Three clean commits instead of four.
reword a1b2c3d Add user model
pick b2c3d4e Add user controller
Save and close. Git will open a new editor for the commit message of a1b2c3d.
edit a1b2c3d Add user model
pick b2c3d4e Add user controller
Save and close. Git stops at that commit:
# Add the forgotten file
git add user_model_test.js
git commit --amend --no-edit
git rebase --continueSimply rearrange the lines:
pick d4e5f6g Add user routes
pick a1b2c3d Add user model
pick b2c3d4e Add user controller
pick a1b2c3d Add user model
drop b2c3d4e Add experimental debug logging
pick c3d4e5f Add user controller
Or simply delete the line entirely — same effect.
If you name fixup commits with fixup! or squash! prefix, Git can organize them automatically.
# Original commit
git commit -m "Add user model"
# Later, a fix for that commit
git commit -m "fixup! Add user model"
# Rebase with autosquash
git rebase -i --autosquash HEAD~3Git automatically puts the fixup commit right after its target and marks it as fixup.
← Chapter 11: Cherry-Picking · Next: Chapter 13 — Git Internals →