Interactive rebaseWhat is rebase?A Git operation that replays your branch's commits on top of another branch's latest commit, creating a linear history instead of a merge commit. is the editing suite for your commitWhat is commit?A permanent snapshot of your staged changes saved in Git's history, identified by a unique hash and accompanied by a message describing what changed. history. Regular rebase replays your commits automatically. Interactive rebase pauses and asks you what to do with each one: keep it, change its message, combine it with another, delete it, or reorder it. It is the tool that turns a messy series of "WIP" commits into a clean, professional history.
Think of it like editing a draft. Your first pass is rough: typos, reorganizations, false starts. Interactive rebase lets you polish everything before anyone else sees it. The final version reads like you wrote it perfectly the first time.
Starting an interactive rebaseWhat is rebase?A Git operation that replays your branch's commits on top of another branch's latest commit, creating a linear history instead of a merge commit.
# Modify last 3 commits
git rebase -i HEAD~3
# Modify all commits since branching from main
git rebase -i mainYour default text editor opens with a list of commits:
pick abc1234 Add login form
pick def5678 WIP: fix typo in validation
pick ghi9012 WIP: debugging auth
pick jkl3456 Finalize login feature
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# s, squash = use commit, but meld into previous commit
# f, fixup = like squash, but discard this commit's log message
# d, drop = remove commit
# e, edit = use commit, but stop for amendingChange the action word at the start of each line, save, and close the editor. Git executes your instructions top to bottom.
The six actions
| Action | What it does | When to use |
|---|---|---|
pick | Keep the commit as-is | Default, for commits that are already good |
reword | Keep the commit, edit its message | Fix typos or improve descriptions |
squash | Combine with previous commit, edit combined message | Merge related commits together |
fixup | Combine with previous commit, discard this message | Clean up "WIP" and "fix" commits |
drop | Delete the commit entirely | Remove accidental or unwanted commits |
edit | Pause at this commit for modifications | Split a commit or change its content |
Practical examples
Cleaning up WIP commits
The most common use case. You have messy commits from a coding sessionWhat is session?A server-side record that tracks a logged-in user. The browser holds only a session ID in a cookie, and the server looks up the full data on each request.:
pick abc1234 Add authentication system
pick def5678 WIP: fix typo
pick ghi9012 WIP: debugging console.log
pick jkl3456 WIP: another fix
pick mno7890 Finalize auth testsChange to:
pick abc1234 Add authentication system
fixup def5678 WIP: fix typo
fixup ghi9012 WIP: debugging console.log
fixup jkl3456 WIP: another fix
pick mno7890 Finalize auth testsResult: 2 clean commits instead of 5 messy ones. The three WIP commits are absorbed into "Add authenticationWhat is authentication?Verifying who a user is, typically through credentials like a password or token. system" with their messages discarded.
fixup instead of squash when you do not care about the WIP commit messages. squash opens an editor to combine the messages; fixup silently uses the previous commit's message.Fixing a commitWhat is commit?A permanent snapshot of your staged changes saved in Git's history, identified by a unique hash and accompanied by a message describing what changed. message
You wrote "ad login" instead of "add login form":
git rebase -i HEAD~3Change:
pick abc1234 ad loginTo:
reword abc1234 ad loginSave and close. Another editor opens where you fix the message to "feat: add login form".
Deleting an accidental commit
You committed a file with passwords:
pick abc1234 Add feature
pick def5678 Oops: committed .env file
pick ghi9012 Continue feature workChange to:
pick abc1234 Add feature
drop def5678 Oops: committed .env file
pick ghi9012 Continue feature workThe commit and its changes are permanently removed from history.
Reordering commits
Sometimes commits make more sense in a different order:
pick abc1234 Add API endpoint
pick def5678 Add database schemaSwap them so the schemaWhat is schema?A formal definition of the structure your data must follow - which fields exist, what types they have, and which are required. comes first:
pick def5678 Add database schema
pick abc1234 Add API endpointSplitting a commitWhat is commit?A permanent snapshot of your staged changes saved in Git's history, identified by a unique hash and accompanied by a message describing what changed.
You made one big commit that should be two separate ones:
git rebase -i HEAD~2Mark the commit as edit:
pick abc1234 First commit
edit def5678 Big commit with multiple changesGit stops at that commit. Now split it:
# Undo the commit but keep changes in working directory
git reset HEAD~1
# Stage and commit separately
git add src/auth.js
git commit -m "feat: add authentication module"
git add tests/auth.test.js
git commit -m "test: add authentication tests"
# Continue the rebase
git rebase --continueQuick reference
| Task | Steps |
|---|---|
| Clean up WIP commits | git rebase -i HEAD~n, change pick to fixup on WIP lines |
| Fix a commit message | git rebase -i HEAD~n, change pick to reword |
| Delete a commit | git rebase -i HEAD~n, change pick to drop |
| Combine commits | git rebase -i HEAD~n, change pick to squash or fixup |
| Split a commit | git rebase -i HEAD~n, change pick to edit, then git reset HEAD~1 |
| Reorder commits | git rebase -i HEAD~n, rearrange the lines |