git rebase -i: clean up your commits before opening the PR
A step-by-step guide to interactive git rebase: what pick, reword, squash, fixup, drop, and edit do, how to combine commits, and how to abort without breaking anything.
git rebase -i HEAD~3 opens an editor with your last three commits, one per line, with the word pick in front of each one. You change that word to something else (squash, reword, fixup, drop), save, close the editor, and Git rewrites those commits following your instructions. That’s the entire mechanism.
The typical case looks like this: you’ve spent an entire afternoon committing with messages like “wip” and “now it works”, and you want the pull request to read as if you’d gotten it right the first time. I’m assuming you already know what rebase does under the hood. If not, start with what rebase does and when to use it and come back here afterward.
How to use interactive git rebase step by step
-
Check how many commits you want to touch. Run
git log --oneline -6and count down from the top to the last commit you want to modify. That number is the N inHEAD~N. -
Start the rebase. The
-i(for interactive) is what makes the editor open instead of applying the rebase directly.# Opens the instruction list with the last 3 commits git rebase -i HEAD~3 -
Read the list before touching anything. You’ll get something like this, with help comments underneath:
pick 8f3c1a2 add login endpoint pick 2b91d0e wip pick c4e7f10 typo in error messageNotice the order: the oldest commit goes at the top. It’s exactly backwards from
git log, and it’s the first source of confusion. That list is the script Git is going to execute from top to bottom. -
Change the
picks to whatever you want to do with each commit. You can write the full command (squash) or just its initial (s). You can also reorder the lines by cutting and pasting, and Git will apply the commits in the new order. -
Save and close. That’s when Git starts working. If you set any
rewordorsquash, a second editor will open so you can write the final message for the resulting commit.
When it’s done, git log --oneline gives you back the now-clean history. The new commits have different hashes than before even though the content is identical: Git hasn’t moved your commits, it’s created copies.
What each editor command does
| Command | What happens to that commit |
|---|---|
pick (p) | Applied as-is. It’s the default value for every line. |
reword (r) | Applied the same way, but Git opens the editor so you can change the message. Perfect for the classic “fix bug” with no further context. |
squash (s) | Merges into the commit on the line above. Git opens an editor with both messages together so you can write a single one. |
fixup (f) | Merges into the one above just like squash, but throws its message away without asking. |
drop (d) | That commit disappears from the history, changes and all. |
edit (e) | Git stops right after applying it and hands the terminal back to you so you can modify files, split the commit in two, or whatever you need. You continue with git rebase --continue. |
There are more commands in that list, like exec for running a shell command between commits or break for pausing the rebase wherever you say [2]. With the six in the table you can handle any normal branch cleanup.
Example with squash and fixup: combining four commits into two
You have this on your branch:
pick 8f3c1a2 add login endpoint
pick 2b91d0e wip
pick c4e7f10 typo
pick 5a0b3d9 add login tests
The first three are really a single piece of work, and the first message is fine. The two in the middle add nothing to the history. The last one deserves to stand on its own. You leave it like this:
pick 8f3c1a2 add login endpoint
fixup 2b91d0e wip
fixup c4e7f10 typo
reword 5a0b3d9 add login tests
You save, close, and Git opens the editor once so you can rewrite the message for the tests commit. Result: two commits, each with a message a reviewer can understand without opening the diff.
If you do this often, there’s a shortcut that skips the editor entirely. When you fix something from an earlier commit, instead of a new commit run git commit --fixup=8f3c1a2, and then git rebase -i --autosquash HEAD~4 places that fix right below its commit with fixup already marked.
Interactive rebase is only one way to combine commits: git squash: what it is and the four ways to do it covers the others and what breaks afterward.
Conflicts, —abort, and the safety net
A conflict doesn’t cancel anything: the rebase stops at the line on the list that caused it and waits. You resolve it right there and tell it to continue with the remaining lines:
git add src/auth.ts # mark the conflict as resolved
git rebase --continue # resume the list at the next line
If you’d rather bail out, git rebase --abort undoes the entire list and leaves the branch exactly as it was before you started, whether you’d resolved three commits or none.
Once the rebase has finished, --abort is no longer available and the safety net becomes git reflog, which lets you go back to any position the branch has been in. You can read how in the git reflog guide.
Common mistakes
Rebasing commits you’ve already pushed
This is the one rule on this page with no gray area: if those commits are already on a branch other people share, don’t touch them. Every line you mark with squash, fixup, or reword changes that commit’s hash, and everyone else is left with a history that no longer matches the server’s. The official Git documentation says this outright [1].
On your own feature branch, even if it’s already pushed, you can do this: when you’re done you push it with git push --force-with-lease. I break down exactly where that line sits in merge vs rebase.
Putting squash on the first line
If you mark squash or fixup on the very first line, Git fails with a cannot 'squash' without a previous commit. Makes sense: those two commands merge the commit into the one on the line above, and there isn’t one there. If what you’re after is merging the first item on the list with the commit before it, include one more commit in the range: HEAD~4 instead of HEAD~3.
Miscounting the N in HEAD~N
Going too far back is the fastest way to end up rewriting work that was already on main. Always check git log --oneline first and count the lines.
Using —skip to get out of a conflict
git rebase --skip doesn’t skip the conflict, it skips the entire commit that was causing it. If you use it just to make the red message go away, you lose those changes and won’t find out until much later.
Checklist before you run the rebase
- The commits I’m about to touch aren’t on
mainor any shared branch - I counted the N with
git log --oneline, not from memory - The working tree is clean (
git statusshows no uncommitted changes) - I know how to save and exit the editor I have configured
- I’m clear that if in doubt,
git rebase --aborttakes me back to my starting point
With this you can leave any branch presentable in a couple of minutes. If Git in general still feels like a black box that only works when you copy commands from Stack Overflow, in the course Git: from Zero to Professional we walk through exactly this with interactive exercises, rebase included.
Sources
- Git Tools: Rewriting History — Pro Git (Chacon & Straub) — the official warning against rewriting commits already pushed to a shared repository.
- git-rebase documentation — git-scm.com — definitions of the interactive-list commands (
pick,reword,edit,squash,fixup,drop,exec,break) and of--continue,--abort,--skip, and--autosquash.
Frequently asked questions
What’s the difference between squash and fixup in git rebase -i?
Both merge the commit into the one on the line above; the difference is that squash lets you write the final message and fixup discards the absorbed commit’s message without asking.
How do I exit the rebase editor without breaking anything?
It depends on which editor Git has configured. In vim it’s Esc, then :wq and Enter to save and quit; in nano, Ctrl+O, Enter, and Ctrl+X. If you delete every line on the list and save, Git responds “Nothing to do” and cancels the rebase without touching your branch, so that’s the clean emergency exit. And if the rebase is already underway, git rebase --abort takes you back to the initial state.
Can I do interactive git rebase on commits I’ve already pushed?
On your own personal branch, yes, by pushing it again afterward with git push --force-with-lease. On a shared branch, no, for the reasons I explain above under common mistakes.
How do I undo an interactive git rebase that went wrong?
If it’s still in progress, git rebase --abort. If it’s already finished, look in the reflog for the position before it and go back to it:
git reflog # find the line right before the rebase
git reset --hard HEAD@{5} # return the branch to that position
Can I reorder commits with git rebase -i?
Yes: cut and paste the editor’s lines into whatever order you want and Git applies the commits following the new list, with a conflict exactly where one depends on another that’s now behind it.