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.
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:
git log --onelineb06ddb2 add serch
4fd78ca fix typo
77610fd wip
288c23b Add login
7e555d1 Start projectTo edit the last four commits, run:
git rebase -i HEAD~4Git opens your editor with one line for each commit, oldest first:
pick 288c23b Add login
pick 77610fd wip
pick 4fd78ca fix typo
pick b06ddb2 add serchYou change the word at the start of a line, save, and close the editor. Git then carries out the list from top to bottom.
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:
pick 288c23b Add login
fixup 77610fd wip
fixup 4fd78ca fix typo
reword b06ddb2 add serchAfter you close the editor, Git stops to ask for the new message of the reword commit. Here we type Add search. The result:
git rebase -i HEAD~4
git log --oneline[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 projectFive 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:
echo "login limit" >> login.txt
git commit -q -a --fixup=HEAD~1
git log --onelineb577725 fixup! Add login
0eefbbb Add search
f0f9899 Add login
7e555d1 Start projectLater, --autosquash moves it to the correct place and marks it as fixup for you:
git rebase -i --autosquash HEAD~3
git log --onelineSuccessfully rebased and updated refs/heads/main.
1e8712c Add search
fb44420 Add login
7e555d1 Start projectEdit an older commit#
Mark a commit as edit, and the rebase pauses there. Change files, then run:
git add .
git commit --amend
git rebase --continueSquash everything on a branch#
To turn a whole branch into one commit, use git merge-base to find where it split from main:
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:
git rebase --abortIf 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
git reset --soft HEAD~2
git commit -q -m "Second and third"
git log --oneline2a736f5 Second and third
a8e7226 FirstKey takeaway#
Next: Cherry-Pick.
Finished this lesson?