Skip to content
Laviyx Learn

Try

Git and GitHub course menu

Interactive Rebase: Squash, Reword and Reorder Git Commits

Clean up your commit history with git rebase -i: squash commits, reword messages, drop and reorder commits, and use fixup commits with --autosquash before opening a pull request.

Advanced7 min readUpdated
On this page

Work in progress is messy. Before others read your branch, you can tidy it with interactive rebase. It lets you edit, combine, reorder or delete commits on your own branch.

Start#

Here is a branch that needs tidying:

Terminal
git log --oneline
Output
b06ddb2 add serch
4fd78ca fix typo
77610fd wip
288c23b Add login
7e555d1 Start project

To edit the last four commits, run:

Terminal
git rebase -i HEAD~4

Git opens your editor with one line for each commit, oldest first:

Text
pick 288c23b Add login
pick 77610fd wip
pick 4fd78ca fix typo
pick b06ddb2 add serch

You change the word at the start of a line, save, and close the editor. Git then carries out the list from top to bottom.

CommandWhat it does
pickKeep the commit
rewordKeep it, but change its message
squashMerge it into the commit above, and combine the messages
fixupMerge it into the commit above, and discard this message
editStop at this commit so that you can change it
dropDelete the commit

You can also change the order by moving lines.

Squash commits#

We want wip and fix typo to become part of Add login, so we mark them as fixup. We also want to fix the spelling of the last message, so we mark it as reword. The edited list is:

Text
pick 288c23b Add login
fixup 77610fd wip
fixup 4fd78ca fix typo
reword b06ddb2 add serch

After you close the editor, Git stops to ask for the new message of the reword commit. Here we type Add search. The result:

Terminal
git rebase -i HEAD~4
git log --oneline
Output
[detached HEAD 0eefbbb] Add search
 Date: Thu Nov 5 09:20:00 2026 +0530
 1 file changed, 1 insertion(+)
 create mode 100644 search.txt
Successfully rebased and updated refs/heads/main.
0eefbbb Add search
f0f9899 Add login
7e555d1 Start project

Five messy commits became three clear ones, and the final file content has not changed.

Fixup commits#

If you find a mistake in an earlier commit, you can mark the fix as you make it. --fixup names the commit to repair:

Terminal
echo "login limit" >> login.txt
git commit -q -a --fixup=HEAD~1
git log --oneline
Output
b577725 fixup! Add login
0eefbbb Add search
f0f9899 Add login
7e555d1 Start project

Later, --autosquash moves it to the correct place and marks it as fixup for you:

Terminal
git rebase -i --autosquash HEAD~3
git log --oneline
Output
Successfully rebased and updated refs/heads/main.
1e8712c Add search
fb44420 Add login
7e555d1 Start project

Edit an older commit#

Mark a commit as edit, and the rebase pauses there. Change files, then run:

Terminal
git add .
git commit --amend
git rebase --continue

Squash everything on a branch#

To turn a whole branch into one commit, use git merge-base to find where it split from main:

Terminal
git rebase -i $(git merge-base main HEAD)

Or leave the branch as it is and use Squash and merge on the pull request.

If it goes wrong#

While the rebase is running, cancel it:

Terminal
git rebase --abort

If you have finished and regret it, the old commits can still be found with git reflog.

Common mistakes#

Practice#

Practice exercise

Without an editor, combine the last two commits into one using git reset --soft, a common alternative to squashing. Keep the message Second and third.

Show hint

git reset --soft HEAD~2 moves the branch back but keeps the changes staged.

Show solution
Terminal
git reset --soft HEAD~2
git commit -q -m "Second and third"
git log --oneline
Output
2a736f5 Second and third
a8e7226 First

Key takeaway#

Next: Cherry-Pick.

Finished this lesson?