Interactive Rebase: Squash, Fixup, Reword, Reorder, Edit and Autosquash
Clean up commits before sharing them with git rebase -i: the todo list, squashing and fixing up, rewording, reordering, dropping, editing and splitting commits, autosquash with fixup commits, exec and break, rebasing from the root, aborting, and the new git history command.
kvi5hnuCode Sprout team member9 min readOn this page
Real work produces messy commits: "wip", "fix typo", "really fix it", changes to the same file spread across five commits. Before others review or merge your branch, you can rewrite those commits into a clean story: combine them, fix messages, change their order, remove mistakes, or split a commit that did two things. Interactive rebase, git rebase -i, does all of this. Like any rebase, it creates new commits, so use it on commits that are still yours (article 14).
The todo list
git rebase -i <base> takes the commits after <base> and opens your editor with a todo list, one line per commit, oldest first:
git init -q trail-guide && cd trail-guide
echo "# Trail guide" > README.md; git add .; git commit -q -m "Start project"
echo "Lake path: 5 km" > lake.txt; git add .; git commit -q -m "Add lake page"
echo "Boats for hire." >> lake.txt; git commit -q -am "wip"
echo "Ridge loop: 8 km" > ridge.txt; git add .; git commit -q -m "Add ridge page"
echo "Open May to September." >> lake.txt; git commit -q -am "fix lake page"
git log --oneline
echo "=== todo list for git rebase -i HEAD~4 (help comments trimmed)"
GIT_SEQUENCE_EDITOR="sed -n '/^[^#]/p; /^# Commands/,/^# d, drop/p'" git rebase -q -i HEAD~4
242bf05 fix lake page
720ef25 Add ridge page
a2b4912 wip
d2a3ed1 Add lake page
7fb62a0 Start project
=== todo list for git rebase -i HEAD~4 (help comments trimmed)
pick d2a3ed1 # Add lake page
pick a2b4912 # wip
pick 720ef25 # Add ridge page
pick 242bf05 # fix lake page
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup [-C | -c] <commit> = like "squash" but keep only the previous
# commit's log message, unless -C is used, in which case
# keep only this commit's message; -c is same as -C but
# opens the editor
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
Note the order: git log shows newest first, while the todo list shows oldest first, the order in which Git will replay the commits. You edit the list, save and close the editor, and Git carries out the commands from top to bottom.
HEAD~4 is the commit four steps back. The rebase lists the commits after it, so the last four commits. On a feature branch, git rebase -i main lists all the branch's commits, which is usually what you want. In the sandbox, the GIT_SEQUENCE_EDITOR variable replaces the editor for the todo list with a script. Here it just prints the list, leaving it unchanged, so the rebase changes nothing.
The commands you'll use most:
| Command | Effect |
|---|---|
pick (p) | keep the commit as it is |
reword (r) | keep it, and stop to edit its message |
edit (e) | keep it, and stop after it so you can amend it or split it |
squash (s) | combine it with the commit above, and edit the combined message |
fixup (f) | combine it with the commit above, keeping only the above commit's message. fixup -C keeps this commit's message instead |
drop (d) | remove the commit. Deleting its line does the same |
exec (x) | run a shell command at that point |
break (b) | stop at that point, so you can look around, then git rebase --continue |
Moving a line moves the commit. Deleting every line aborts the rebase.
Squash and fixup: combining commits
To fold "wip" and "fix lake page" into "Add lake page", move "fix lake page" up below it, and change both to fixup:
pick d2a3ed1 # Add lake page
fixup a2b4912 # wip
fixup 242bf05 # fix lake page
pick 720ef25 # Add ridge page
git init -q trail-guide && cd trail-guide
echo "# Trail guide" > README.md; git add .; git commit -q -m "Start project"
echo "Lake path: 5 km" > lake.txt; git add .; git commit -q -m "Add lake page"
echo "Boats for hire." >> lake.txt; git commit -q -am "wip"
echo "Ridge loop: 8 km" > ridge.txt; git add .; git commit -q -m "Add ridge page"
echo "Open May to September." >> lake.txt; git commit -q -am "fix lake page"
# The todo list we want, written by a script instead of by hand in an editor
lake=$(git rev-parse --short HEAD~3) wip=$(git rev-parse --short HEAD~2)
ridge=$(git rev-parse --short HEAD~1) fix=$(git rev-parse --short HEAD)
printf 'pick %s\nfixup %s\nfixup %s\npick %s\n' $lake $wip $fix $ridge > ../todo.txt
cat ../todo.txt
GIT_SEQUENCE_EDITOR="cp ../todo.txt" git rebase -q -i HEAD~4
echo "=== after"
git log --oneline --stat --format='%h %s'
pick d2a3ed1
fixup a2b4912
fixup 242bf05
pick 720ef25
=== after
c80b4fd Add ridge page
ridge.txt | 1 +
1 file changed, 1 insertion(+)
a97d1cd Add lake page
lake.txt | 3 +++
1 file changed, 3 insertions(+)
7fb62a0 Start project
README.md | 1 +
1 file changed, 1 insertion(+)
The branch now has two commits, each complete. With squash instead of fixup, Git would stop and open the editor with all the messages combined, for you to write the final message.
Moving a commit can cause conflicts, if a later commit depends on changes made by one you moved past it. Git stops, and you resolve them as in any rebase (article 14), or abort.
Reword: fixing messages
Change pick to reword for each commit whose message needs work. Git stops at each one and opens the editor with the message. For the last commit only, git commit --amend is simpler (article 6).
git init -q trail-guide && cd trail-guide
echo "# Trail guide" > README.md; git add .; git commit -q -m "Start project"
echo "Lake path: 5 km" > lake.txt; git add .; git commit -q -m "stuff"
echo "Ridge loop: 8 km" > ridge.txt; git add .; git commit -q -m "Add ridge page"
# Mark the "stuff" commit for reword; a second script plays the message editor
GIT_SEQUENCE_EDITOR="sed -i '/ stuff$/s/^pick/reword/'" \
GIT_EDITOR="sed -i '1s/.*/Add lake page/'" \
git rebase -q -i HEAD~2
git log --oneline
[detached HEAD d2a3ed1] Add lake page
Date: Mon Mar 2 10:00:00 2026 +0000
1 file changed, 1 insertion(+)
create mode 100644 lake.txt
bead40d Add ridge page
d2a3ed1 Add lake page
7fb62a0 Start project
Edit: changing or splitting a commit
edit stops the rebase just after that commit is applied. You can then:
- change the commit: edit files,
git add,git commit --amend, thengit rebase --continue; - split it: undo the commit but keep its changes with
git reset HEAD~, commit them in pieces, thengit rebase --continue; - insert a new commit after it: make and commit changes, then continue.
git init -q trail-guide && cd trail-guide
echo "# Trail guide" > README.md; git add .; git commit -q -m "Start project"
echo "Lake path: 5 km" > lake.txt; echo "Ridge loop: 8 km" > ridge.txt
git add .; git commit -q -m "Add lake and ridge pages"
echo "body { margin: 0 }" > style.css; git add .; git commit -q -m "Add stylesheet"
GIT_SEQUENCE_EDITOR="sed -i '/Add lake and ridge/s/^pick/edit/'" git rebase -q -i HEAD~2
echo "=== stopped after the commit"
git status | head -3
git reset -q HEAD~ # undo the commit, keep its changes unstaged
git add lake.txt; git commit -q -m "Add lake page"
git add ridge.txt; git commit -q -m "Add ridge page"
git rebase --continue
echo "=== after"
git log --oneline
Stopped at 58a488a... # Add lake and ridge pages
You can amend the commit now, with
git commit --amend
Once you are satisfied with your changes, run
git rebase --continue
=== stopped after the commit
interactive rebase in progress; onto 7fb62a0
Last command done (1 command done):
edit 58a488a # Add lake and ridge pages
=== after
80851ef Add stylesheet
bead40d Add ridge page
d2a3ed1 Add lake page
7fb62a0 Start project
To split within a file, use git add -p after the reset to stage some hunks for the first commit (article 5).
Autosquash: planning fixups as you go
Often you notice a problem in an earlier commit while working. Instead of remembering to fix it later, commit the fix as a fixup commit for that earlier commit:
git commit --fixup=<commit> # message "fixup! <that commit's subject>"
git commit --fixup=:/lake # the newest commit whose message matches "lake"
git commit --squash=<commit> # "squash! ...": combine, and edit the message
git commit --fixup=amend:<commit> # "amend! ...": combine, and replace the message
git commit --fixup=reword:<commit> # change only the message, with no file changes
Then git rebase -i --autosquash main builds the todo list for you: each fixup! commit is moved under its target and marked fixup. You just save the list:
git init -q trail-guide && cd trail-guide
echo "# Trail guide" > README.md; git add .; git commit -q -m "Start project"
echo "Lake path: 5 km" > lake.txt; git add .; git commit -q -m "Add lake page"
echo "Ridge loop: 8 km" > ridge.txt; git add .; git commit -q -m "Add ridge page"
echo "Boats for hire." >> lake.txt
git add lake.txt
git commit -q --fixup=:/"lake page"
git log --oneline
echo "=== the todo list autosquash prepares"
GIT_SEQUENCE_EDITOR="sed -n '/^[^#]/p'" git rebase -q -i --autosquash HEAD~3
echo "=== after"
git log --oneline
d5c15c9 fixup! Add lake page
bead40d Add ridge page
d2a3ed1 Add lake page
7fb62a0 Start project
=== the todo list autosquash prepares
pick d2a3ed1 # Add lake page
fixup d5c15c9 # fixup! Add lake page
pick bead40d # Add ridge page
=== after
60915a4 Add ridge page
67b700a Add lake page
7fb62a0 Start project
:/text searches commit messages, newest first, so :/"lake page" found "Add lake page". sed -n printed the list but left the file unchanged, so the rebase carried out the prepared list. Set rebase.autoSquash=true to make --autosquash the default for interactive rebases. This habit, small fixup commits during work and one autosquash before review, keeps branches clean with little effort.
exec, break and testing
exec <command>lines run a shell command at that point in the list. If the command fails, the rebase stops there.git rebase -i -x "make test" mainadds anexecline after every commit, so each commit is tested as it's replayed (article 14).breakpauses the rebase at that point, for example to inspect the state, thengit rebase --continueresumes.git rebase --edit-todoreopens the remaining list while a rebase is paused.
Rebasing from the first commit
HEAD~n can't reach past the first commit. To include it, use git rebase -i --root, which lists every commit in the branch, including the root commit.
Stopping and undoing
git rebase --abortat any pause returns the branch to where it was.- Deleting every line of the todo list, or exiting the editor with an error (
:cqin Vim), aborts before anything happens. - After a rebase finishes,
git reset --hard ORIG_HEADor the reflog undoes it (articles 14 and 26).
Merges and stacked branches in the todo list
With --rebase-merges, the todo list can also contain label, reset and merge commands, which rebuild merge commits. With --update-refs, it contains update-ref lines that move other branches pointing into the rebased commits. You rarely edit those lines by hand. Leave them in place unless you know you want to change them.
The git history command (experimental)
Git 2.54 to 2.56 added an experimental git history command for the most common single-commit edits, without writing a todo list:
| Subcommand | Effect | Since |
|---|---|---|
git history reword <commit> | edit a commit's message | 2.54 |
git history split <commit> [-- paths] | choose hunks interactively for a first commit; the rest becomes a second commit | 2.54 |
git history fixup <commit> | fold the staged changes into that commit | 2.55 |
git history drop <commit> | remove a commit, replaying its descendants onto its parent | 2.56 |
Later commits are replayed on top automatically, and by default other branches pointing at rewritten commits are updated too. --dry-run shows the reference updates without making them. Unlike a rebase, it doesn't stop for conflicts: if one would occur, it refuses and changes nothing.
git init -q trail-guide && cd trail-guide
echo "# Trail guide" > README.md; git add .; git commit -q -m "Start project"
echo "Lake" > lake.txt; echo "Ridge" > ridge.txt; git add .; git commit -q -m "Add lake and ridge pages"
echo "body { margin: 0 }" > style.css; git add .; git commit -q -m "Add stylesheet"
echo "temp" > debug.txt; git add .; git commit -q -m "Add debug notes"
echo "=== drop the last commit"
git history drop HEAD
git log --oneline
echo "=== split: take lake.txt's hunk for the first commit"
printf 'y\n' | git history split HEAD~1 -- lake.txt > /dev/null
GIT_EDITOR="sed -i '1s/.*/Add lake page/'" git history reword HEAD~2
GIT_EDITOR="sed -i '1s/.*/Add ridge page/'" git history reword HEAD~1
git log --oneline --stat --format='%h %s'
echo "=== fixup: fold a staged change into the lake commit"
echo "Lake path: 5 km" > lake.txt
git add lake.txt
git history fixup HEAD~2
git show --format='%h %s' HEAD~2
=== drop the last commit
e715828 Add stylesheet
25c93ee Add lake and ridge pages
7fb62a0 Start project
=== split: take lake.txt's hunk for the first commit
4e7d284 Add stylesheet
style.css | 1 +
1 file changed, 1 insertion(+)
68f0720 Add ridge page
ridge.txt | 1 +
1 file changed, 1 insertion(+)
340af72 Add lake page
lake.txt | 1 +
1 file changed, 1 insertion(+)
7fb62a0 Start project
README.md | 1 +
1 file changed, 1 insertion(+)
=== fixup: fold a staged change into the lake commit
d2a3ed1 Add lake page
diff --git a/lake.txt b/lake.txt
new file mode 100644
index 0000000..a5822b6
--- /dev/null
+++ b/lake.txt
@@ -0,0 +1 @@
+Lake path: 5 km
The split asks about each hunk, like git add -p: answering y put lake.txt in the first commit, and both new commits kept the original message, so reword fixed them. Because git history is marked experimental, its options may still change. Interactive rebase remains the general tool.
A cleanup routine before review
git fetchand look at your branch:git log --oneline origin/main...git rebase -i --autosquash origin/main: squash fixups, reword vague messages, drop debug commits, and order the commits so each makes sense on its own.- Run the tests, ideally on every commit with
-x. - Push with
git push --force-with-leaseif the branch was already pushed (article 20).
The goal is commits a reviewer can read one at a time, each building, each with a message that says why.
Common questions
What's the difference between squash and fixup?
Both combine a commit with the one above it. squash lets you edit the combined message, starting from both. fixup silently keeps the earlier commit's message, which is right for "fix typo" commits.
Why are the commits listed in the opposite order to git log?
The todo list is a script that Git runs from top to bottom, so it starts with the oldest commit, the first to be replayed.
I squashed the wrong commits. Can I undo it?
Yes. After the rebase, git reset --hard ORIG_HEAD. Or find the branch's previous position in git reflog and reset to it (article 26).
Can I squash commits without an interactive rebase?
Yes. git reset --soft HEAD~3 then git commit replaces the last three commits with one, containing all their changes. You write a new message. git merge --squash does something similar across branches (article 12).
Should I squash everything into one commit? Not necessarily. One commit per logical change is the goal. Squash noise, such as typo fixes and "wip", but keep separate steps that a reviewer benefits from seeing separately.
Summary
git rebase -i <base>opens a todo list of the commits after<base>, oldest first. Edit it, save it, and Git replays it.pickkeeps a commit,rewordchanges its message,editstops to amend or split it,squashandfixupcombine it with the one above, anddropremoves it. Moving lines reorders commits.git commit --fixup=<commit>plusgit rebase -i --autosquasharranges fixups automatically. Setrebase.autoSquash=true.execruns commands,breakpauses,--rootincludes the first commit, and--abortorORIG_HEADundoes.- Git 2.54 to 2.56's experimental
git historyrewords, splits, fixes up and drops single commits without a todo list. - Rewrite only commits that are still yours.
Practice questions
Your todo list is below. What does the resulting history look like?
pick a1 Add lake page
squash b2 Add boat hire
pick c3 Add ridge page
drop d4 Add debug output
Answer
Two commits: the first combines "Add lake page" and "Add boat hire", with a message you write in the editor (starting from both messages), followed by "Add ridge page". The debug commit is removed. All the resulting commits have new IDs.
You're working on a branch and notice that the commit "Add ridge page", three commits back, has a wrong distance. How do you fix it with autosquash?
Answer
Edit the file, stage it, and commit it as a fixup for that commit, then autosquash:
git add ridge.txt
git commit --fixup=:/"ridge page"
git rebase -i --autosquash main # save the prepared list
The fixup commit is moved under "Add ridge page" and folded into it.
During git rebase -i, you marked a commit edit. Git stopped. How do you split that commit into two?
Answer
Undo the commit but keep its changes with git reset HEAD~, then stage and commit the first part (git add <files> or git add -p, then git commit), stage and commit the rest, and run git rebase --continue.
Previous: Rebasing: Replaying Commits for a Linear History
Next: Cherry-Picking Commits and Finding What's Missing Between Branches

More in Git Course
Quiz: Git Delicate Details
Twenty-five questions on Git's subtle corners: what commit -a and stash leave out, empty folders, ignore patterns and negation, HEAD~ versus HEAD^, ours and theirs in a rebase, tags that don't move or don't push, describe, merge reverts, diff exit codes, stash conflicts and more.
Quiz: Git Interview Questions
Thirty Git questions of the kind asked in developer interviews: Git versus GitHub, the staging area, merge versus rebase, fetch versus pull, reset, revert and restore, detached HEAD, conflicts, force pushing, stash, cherry-pick, reflog, bisect, submodules, LFS and how commits are identified.
Quiz: Git Course Final Exam (All Parts)
The final exam for the Git course: thirty questions across all twelve parts, from the three areas and staging through branching, remotes, undoing, history investigation, configuration, tags, larger projects, patches, rewriting, internals and troubleshooting.
Quiz: Professional Git Practice (Git Part 12)
Sixteen questions on migrating to Git from Subversion, TFVC and Mercurial, team best practices for commits, branches and reviews, repository hygiene, and diagnosing and fixing the most common Git errors, with explanations.
Discussion
Sign in to ask a question or share what you learned.