Interactive Rebase

Updated · 15 min read
hardGit 2.53gitinteractive-rebasesquashfixuprange-diffinterview

Interactive rebase is a form of rebase in which Git first shows you the list of commits it is going to replay, and lets you edit that list. The command is git rebase -i. For each commit you choose an action: keep it, change its message, combine it with another, drop it, stop to change it, or move it to another position. It is used to turn a messy series of work-in-progress commits into a few clear commits before a review.

Writing a report works the same way. While drafting, you save many rough versions: "draft", "fix typo", "add table", "oops". Before you hand it in, you tidy up: merge the small fixes into the sections they belong to, remove the notes to yourself, and put the sections in a sensible order. The finished report is the same work, presented clearly.

Syntax

Terminal
git rebase -i <base>

<base> is the commit before the first one you want to edit. Usually it is main (to edit all of your branch's own commits) or HEAD~<n> (to edit the last n commits). Git opens your editor with a to-do list, one line per commit, oldest first. You edit the list, save and close the editor, and Git carries it out.

CommandShortDescription
pickpIt is used to keep the commit as it is.
rewordrIt is used to keep the commit but change its message.
editeIt is used to stop after this commit, so you can change it (for example, split it).
squashsIt is used to combine the commit into the previous one, and edit the combined message.
fixupfIt is used to combine the commit into the previous one, and keep only the previous message.
dropdIt is used to remove the commit. Deleting the line does the same.
execxIt is used to run a shell command at that point, for example the tests.
breakbIt is used to stop at that point, so you can look around. Continue with git rebase --continue.

Reordering commits means moving their lines. The order of the lines is the order in which they are replayed.

The Starting Point: A Messy Branch

Navika's receipts branch has five commits. Two are fixes to earlier commits, and one is a debugging file that must not be merged:

Terminal
mkdir fee-tracker
cd fee-tracker
git init --quiet
echo "Fee Tracker" > README.md
git add README.md
git commit --quiet -m "Add the README"
git switch --quiet -c feature/receipts
echo "Receipt No. 1" > receipts.txt
git add receipts.txt
git commit --quiet -m "Add receipt numbers"
echo "Print after pament" >> receipts.txt
git commit --quiet -am "wip"
printf "School: Anamaneni Public School\nReceipt No. 1\nPrint after pament\n" > receipts.txt
echo "Receipts show the school name." >> README.md
git commit --quiet -am "Add the school name"
sed -i.bak "s/pament/payment/" receipts.txt
rm receipts.txt.bak
git commit --quiet -am "fix typo"
echo "debug on" > debug.txt
git add debug.txt
git commit --quiet -m "debug output"
git branch backup
git log --oneline main..
Output
7e025fd (HEAD -> feature/receipts, backup) debug output
d76fc54 fix typo
d8a2b67 Add the school name
9e27f95 wip
e5d493c Add receipt numbers

git branch backup keeps a name on the messy version, so it can be compared with the result at the end. main.. lists only the branch's own commits, newest first.

The To-Do List

On your computer, git rebase -i main opens your editor with the list. So that this page can show it, GIT_SEQUENCE_EDITOR=cat replaces the editor with cat for this one command: it prints the list and changes nothing, so the rebase replays every commit as it was:

Terminal
GIT_SEQUENCE_EDITOR=cat git rebase -i main
Output
pick e5d493c # Add receipt numbers
pick 9e27f95 # wip
pick d8a2b67 # Add the school name
pick d76fc54 # fix typo
pick 7e025fd # debug output
 
# Rebase 9d994aa..7e025fd onto 9d994aa (5 commands)
#
# 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
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
#         create a merge commit using the original merge commit's
#         message (or the oneline, if no original merge commit was
#         specified); use -c <commit> to reword the commit message
# u, update-ref <ref> = track a placeholder for the <ref> to be updated
#                       to this position in the new commits. The <ref> is
#                       updated at the end of the rebase
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
Successfully rebased and updated refs/heads/feature/receipts.

Explanation: the list is oldest first, the reverse of git log. Each line is a command, a short commit ID and, after #, the subject for your information. Only the command and the ID matter: you can leave out or change the text after them. Below it, Git prints a short help with every command. Lines starting with # are ignored. Because every line still says pick and nothing changed, Git kept the commits exactly as they were.

Example 1: Reword, Fixup, Reorder and Drop

Navika wants three clean commits:

  1. Keep "Add receipt numbers".
  2. Give "wip" a real message, and fold "fix typo" into it, because the typo was made there.
  3. Keep "Add the school name".
  4. Drop "debug output".

In her editor, she would change the list to the lines below: reword the second commit, move the "fix typo" line up under it and change it to fixup, and drop the last one. So that this page can run it, the edited list is saved in a file, ../plan.txt, and GIT_SEQUENCE_EDITOR copies it over Git's list. The new message for the reword goes into ../message.txt the same way, through GIT_EDITOR:

Terminal
cat > ../plan.txt <<'EOF'
pick e5d493c Add receipt numbers
reword 9e27f95 wip
fixup d76fc54 fix typo
pick d8a2b67 Add the school name
drop 7e025fd debug output
EOF
echo "Print a receipt after payment" > ../message.txt
GIT_SEQUENCE_EDITOR="cp ../plan.txt" GIT_EDITOR="cp ../message.txt" git rebase -i main
Output
[detached HEAD 9c6d590] Print a receipt after payment
 Date: Mon Jan 5 10:12:00 2026 -0600
 1 file changed, 1 insertion(+)
Successfully rebased and updated refs/heads/feature/receipts.
Terminal
git log --oneline main..
cat receipts.txt
ls
Output
7c21ec0 (HEAD -> feature/receipts) Add the school name
5bf490d Print a receipt after payment
e5d493c Add receipt numbers
School: Anamaneni Public School
Receipt No. 1
Print after payment
README.md
receipts.txt

Explanation: Git replayed the list from top to bottom. reword stopped to take the new message, which is the [detached HEAD ...] line: during a rebase, Git commits on a detached HEAD and moves the branch at the end. fixup folded the typo fix into that commit and kept its message. Moving the "fix typo" line before "Add the school name" changed the order of the replay. drop left out the debugging commit, so debug.txt is gone. The result is three clear commits, with the same final receipts.txt content apart from the debug file.

From a messy branch to a clean one

Example 2: Split a Commit with edit

"Add the school name" changed two unrelated things: receipts.txt and the README. It should be two commits. edit stops the rebase right after that commit, so Navika can undo it and commit the parts separately. Here the list only changes one word, so sed makes the edit:

Terminal
GIT_SEQUENCE_EDITOR="sed -i.bak -e 's/^pick \(.*\) Add the school name/edit \1 Add the school name/'" git rebase -i main
Output
Stopped at 7c21ec0...  # Add the school name
You can amend the commit now, with
 
  git commit --amend
 
Once you are satisfied with your changes, run
 
  git rebase --continue

The rebase stopped at "Add the school name". git reset HEAD~ undoes that commit but keeps its changes in the working tree, unstaged. Then each part is committed on its own:

Terminal
git reset --quiet HEAD~
git add receipts.txt
git commit --quiet -m "Add the school name to receipts"
git add README.md
git commit --quiet -m "Describe receipts in the README"
git rebase --continue
Output
Successfully rebased and updated refs/heads/feature/receipts.
Terminal
git log --oneline main..
Output
f4f8cca (HEAD -> feature/receipts) Describe receipts in the README
0bbeda8 Add the school name to receipts
5bf490d Print a receipt after payment
e5d493c Add receipt numbers

Example 3: Test Every Commit with --exec

A clean history should also work at every commit, so that git bisect (later in the course) can test any of them. -x (--exec) adds an exec line after every commit. If the command fails, the rebase stops there. Here, the "test" checks that receipts.txt exists:

Terminal
git rebase -x "test -f receipts.txt" main
Output
Executing: test -f receipts.txt
Executing: test -f receipts.txt
Executing: test -f receipts.txt
Executing: test -f receipts.txt
Successfully rebased and updated refs/heads/feature/receipts.

In a real Java project, the command would be the build and tests, such as -x "mvn -q test".

Compare Before and After: git range-diff

After rewriting, it is good practice to check that the content is what you expected. git range-diff compares two versions of a series of commits, commit by commit:

Terminal
git range-diff main backup feature/receipts
Output
1:  e5d493c = 1:  e5d493c Add receipt numbers
2:  9e27f95 < -:  ------- wip
3:  d8a2b67 < -:  ------- Add the school name
4:  d76fc54 < -:  ------- fix typo
5:  7e025fd < -:  ------- debug output
-:  ------- > 2:  5bf490d Print a receipt after payment
-:  ------- > 3:  0bbeda8 Add the school name to receipts
-:  ------- > 4:  f4f8cca Describe receipts in the README

Explanation: each line pairs an old commit (left) with a new one (right). = means the same change, ! means the change differs (the diff below it shows how), < means only in the old series (dropped), and > means only in the new one (added). Here, only the first commit is paired, because it was kept as it was. The others changed so much (new messages, folded and split) that Git shows them as removed on the left and added on the right. Reviewers use range-diff to see what changed between two versions of a pull request after a force push.

Stopping and Undoing

  • During the rebase: git rebase --abort returns the branch to how it was before. Conflicts are handled as in "Git Rebase": fix, git add, git rebase --continue.
  • After the rebase: ORIG_HEAD points to the branch as it was before the last rebase, so git reset --hard ORIG_HEAD undoes it right away. Later, git reflog lists every earlier position.

Common Mistakes

Choosing a base that is too far back. git rebase -i HEAD~20 on a branch with 3 own commits rewrites commits that are on main too. Use git rebase -i main.

Expecting the list in git log order. It is the other way round: oldest at the top.

squash or fixup on the first line. There is no commit above it to fold into, and Git refuses. The first line must be pick, reword or edit.

Reordering commits that depend on each other. Moving a commit before the one it builds on causes conflicts. Keep dependent commits in order.

Forgetting where you are during an edit stop. git status says which commit you are on and what comes next.

Under the Hood

When you save the list, Git stores it in .git/rebase-merge/git-rebase-todo and works through it line by line, removing each line as it is done (git rebase --edit-todo lets you change the rest while stopped). pick is a cherry-pick. reword is a cherry-pick followed by an amend of the message. fixup and squash apply the change and amend it into the previous commit. edit and break simply stop and wait for --continue. As with every rebase, HEAD is detached until the end, and only then is the branch moved to the last new commit.

Interview Corner

What is interactive rebase used for?

To edit a series of commits before sharing them: change messages (reword), combine commits (squash, fixup), remove them (drop), reorder them, split them (edit), or run tests after each (exec).

What is the difference between squash and fixup?

Both fold a commit into the one before it. squash opens the editor with both messages so you can write the combined message. fixup keeps only the earlier commit's message and discards the folded commit's message.

How do you split a commit into two?

Mark it edit in git rebase -i. When the rebase stops there, run git reset HEAD~ to undo the commit but keep its changes, stage and commit each part separately, then git rebase --continue.

How do you combine the last three commits into one?

git rebase -i HEAD~3, keep the first line as pick and change the other two to squash (or fixup). Alternatively, git reset --soft HEAD~3 and one git commit.

What does git range-diff show?

A commit-by-commit comparison of two versions of a series, such as a branch before and after a rebase, showing which commits are unchanged, changed, removed or added.

Practice

Challenge: one commit for the README. The branch now has four commits. Fold "Describe receipts in the README" into "Add receipt numbers" with fixup, by moving its line up under the first one. Then print the branch's commits and the files changed by the first one.

What does Git print? Before you open the solution, decide how many commits remain, and which files the first commit changes afterward.

Show solution
Terminal
git log --format="%h %s" --reverse main.. > ../current.txt
cat ../current.txt
Output
e5d493c Add receipt numbers
5bf490d Print a receipt after payment
0bbeda8 Add the school name to receipts
f4f8cca Describe receipts in the README

These are the current IDs, oldest first. The plan moves the README commit (the last line) up under the first, as a fixup:

Terminal
cat > ../plan.txt <<EOF
pick $(sed -n 1p ../current.txt)
fixup $(sed -n 4p ../current.txt)
pick $(sed -n 2p ../current.txt)
pick $(sed -n 3p ../current.txt)
EOF
GIT_SEQUENCE_EDITOR="cp ../plan.txt" git rebase -i main
Output
Successfully rebased and updated refs/heads/feature/receipts.
Terminal
git log --oneline main..
git show --stat --format=%s HEAD~2
Output
a1c0ecf (HEAD -> feature/receipts) Add the school name to receipts
b6e1ae5 Print a receipt after payment
aafe2e1 Add receipt numbers
Add receipt numbers
 
 README.md    | 1 +
 receipts.txt | 1 +
 2 files changed, 2 insertions(+)

Three commits remain. The first one, still called "Add receipt numbers", now also changes README.md, because the README commit was folded into it. In your editor, you would simply cut the last line, paste it under the first one, and change pick to fixup.

Checkpoint. In a practice repository, make a branch of eight messy commits ("wip", "fix typo", "oops", and so on) and turn it into three clear commits with one interactive rebase. Compare the two versions with git range-diff.

Quick check: Interactive rebase

4 questions
1In which order does git rebase -i list the commits?
2Which command keeps a commit but lets you change its message?
3You want to fold a "fix typo" commit into the commit before it, keeping that commit's message. Which command?
4How do you undo an interactive rebase right after it finished?

Key Takeaways

Quick reference

CommandIt is used to
git rebase -i mainEdit all of the branch's own commits
git rebase -i HEAD~3Edit the last three commits
git rebase --edit-todoChange the remaining list while stopped
git rebase --continue / --abortGo on, or cancel
git rebase -x "<command>" mainTest every commit
git range-diff main old newCompare two versions of a branch
git reset --hard ORIG_HEADUndo the last rebase

Was this tutorial helpful?

Lesson 58 of 106