Dev tools and workflow · Advanced · 20 min
Clean up your commits before a pull request with interactive rebase
By Padding · Updated 18 Nov 2026
Ubuntu 26.04 · Git 2.56.1 (ppa:git-core/ppa)
You will turn a branch full of "wip" and "fix typo" commits into three clean commits with git rebase -i, fold in review fixes with --autosquash, keep a stacked branch in step with --update-refs, recover from a bad rebase and push the result safely.
On this page
Reviewers read your pull request commit by commit. A branch that says wip, fix typo, really fix and temp debug makes them reconstruct what you meant, and it leaves noise in the project history for anyone who later runs git log or git bisect. Interactive rebase lets you rewrite your own branch before anyone depends on it: merge the fixes into the commits they fix, split a mixed commit in two, reword vague messages and drop what should never have been committed.
You will work in a throwaway repository, ~/git-demo/rebase, with a small shell script called greet.sh. You start with five messy commits and finish with three clean ones, then practise the habits that keep a branch clean through review.
One rule runs through the whole tutorial: rebase rewrites commits, giving each one a new hash. Only do it to commits nobody else has pulled. Your own feature branch before (and often during) review is fine. main, or a branch a teammate has based work on, is not.
Everything up to the final push works offline, so you can tidy a branch on a bus or with your mobile data off and only spend data on one push at the end.
Step 1 · Install Git 2.56
Ubuntu 26.04 ships Git 2.53, which has every feature used here. This tutorial was written against Git 2.56, the current release, which the Git maintainers publish for Ubuntu in the ppa:git-core/ppa repository. To use it, add the repository and upgrade Git:
sudo apt update
sudo apt install -y software-properties-common
sudo add-apt-repository -y ppa:git-core/ppa
sudo apt install -y git
git --versionadd-apt-repository refreshes the package lists itself, so the last install picks up the newer Git. Packages from the PPA get updates through sudo apt upgrade like any other package. On macOS, brew install git gives you a current release.
Check it worked
git --version | grep -E 'git version 2\.(5[6-9]|[6-9][0-9])'The output is git version 2.56.1 or newer. If the command prints nothing, the older Ubuntu package is still in use: run apt policy git and check that the candidate version comes from ppa.launchpadcontent.net.
Step 2 · Make a branch with a messy history
Create the repository with one commit on main, then a feature branch with the kind of history most real branches have:
mkdir -p ~/git-demo/rebase
cd ~/git-demo/rebase
git init
echo "A tiny greeting script" > ABOUT.txt
git add ABOUT.txt
git commit -m "Start the project"
git switch -c feature/greeting
cat > greet.sh <<'EOF'
#!/bin/sh
echo "Helo, wrold"
EOF
git add greet.sh
git commit -m "Add greet script"
cat > README.md <<'EOF'
# greet
Run `sh greet.sh` to print a greeting.
EOF
cat > test.sh <<'EOF'
#!/bin/sh
out=$(sh greet.sh)
if [ "$out" = "Hello, world" ]; then
echo "PASS"
else
echo "FAIL: got '$out'"
exit 1
fi
EOF
git add README.md test.sh
git commit -m "wip"
cat > greet.sh <<'EOF'
#!/bin/sh
echo "Hello, wrold"
EOF
git commit -am "fix typo"
cat > greet.sh <<'EOF'
#!/bin/sh
echo "Hello, world"
EOF
git commit -am "really fix"
echo "remember to delete this" > debug.txt
git add debug.txt
git commit -m "temp debug"The goal is three commits a reviewer can read in order: the script, its documentation, and its test.
Check it worked
git log --oneline main..main.. means "commits on this branch that are not on main". You see five commits, newest first: temp debug, really fix, fix typo, wip and Add greet script.
Step 3 · Squash the fixes, reword and drop with the todo list
Start an interactive rebase of everything since main:
git rebase -i mainGit opens your editor with a todo list, oldest commit first (the reverse of git log). It looks something like this, with your own hashes:
pick 1a2b3c4 Add greet script
pick 2b3c4d5 wip
pick 3c4d5e6 fix typo
pick 4d5e6f7 really fix
pick 5e6f7a8 temp debug
# Rebase 0f1e2d3..5e6f7a8 onto 0f1e2d3 (5 commands)Below that is a comment block listing the commands. Git replays the lines from top to bottom, and the word at the start of each line says what to do with that commit:
pick: keep the commit as it is.reword: keep the changes, and open the editor so you can rewrite the message.edit: apply the commit, then stop so you can change it or split it.git rebase --continuecarries on.squash: merge the commit into the one above it, and open the editor with both messages so you can combine them.fixup: merge the commit into the one above it and throw its message away.fixup -Ckeeps this commit's message instead.drop: leave the commit out. Deleting the line does the same thing.exec <command>: run a shell command at that point, such asexec sh test.sh. The rebase stops if it fails.break: stop here without applying anything, so you can look around.
You can also reorder lines, which reorders the commits. Change the list to this, moving both typo fixes up under the commit they fix:
reword 1a2b3c4 Add greet script
fixup 3c4d5e6 fix typo
fixup 4d5e6f7 really fix
pick 2b3c4d5 wip
drop 5e6f7a8 temp debugSave and close the editor. Because of the reword, Git opens the editor again with the message Add greet script. Replace it with Add a greet script that says hello, save and close. Git applies the two fixups to the reworded commit, replays wip, skips temp debug, and prints Successfully rebased and updated refs/heads/feature/greeting.
If you change your mind while the todo list is open, delete every line and save: Git does nothing. If a rebase stops halfway, git rebase --abort puts the branch back exactly as it was.
Check it worked
git log --oneline main..
cat greet.sh
ls
sh test.shThe log shows two commits, wip and Add a greet script that says hello. The script prints Hello, world with both typos fixed, debug.txt is gone, and the test prints PASS.
Step 4 · Split a mixed commit with edit and reset
The wip commit holds two unrelated changes: the README and the test. Rebase again, and this time mark wip with edit:
git rebase -i mainChange the todo list to:
pick 6f7a8b9 Add a greet script that says hello
edit 7a8b9c0 wipSave and close. Git applies wip and stops with a message such as Stopped at 7a8b9c0... wip. You are now in the middle of the rebase, with wip as the latest commit. Undo that commit while keeping its files, then commit them again in two pieces:
git reset HEAD~
git status --short
git add README.md
git commit -m "Document how to run greet"
git add test.sh
git commit -m "Add a test for greet"
git rebase --continuegit reset HEAD~ moves the branch back one commit and leaves the files in place, so git status --short lists README.md and test.sh as untracked (??). To split changes inside one file, use git add -p to stage it piece by piece. git rebase --continue replays anything left in the todo list, which here is nothing, and finishes.
Check it worked
git log --stat --oneline main..Three commits, each touching one thing: Add a test for greet changes test.sh, Document how to run greet changes README.md, and Add a greet script that says hello changes greet.sh. This is the history you want reviewers to read.
Step 5 · Fold review fixes in with --fixup and --autosquash
During review, someone asks for the script to take a name. The change belongs in the first commit, not in a new address review commit on top. Make the change, and commit it with --fixup, which names the commit it fixes:
cat > greet.sh <<'EOF'
#!/bin/sh
name="${1:-world}"
echo "Hello, $name"
EOF
git commit -a --fixup=":/Add a greet script"
git log --oneline main..:/Add a greet script is Git's syntax for "the newest commit whose message matches this text". You can give a hash instead. The new commit is called fixup! Add a greet script that says hello, and the log now shows four commits.
Now let Git do the reordering:
git rebase -i --autosquash mainThe todo list opens already arranged, with the fix moved under its target:
pick 6f7a8b9 Add a greet script that says hello
fixup 8b9c0d1 fixup! Add a greet script that says hello
pick 9c0d1e2 Document how to run greet
pick 0d1e2f3 Add a test for greetSave and close without changes. Since Git 2.44, git rebase --autosquash main without -i does the same thing without opening the editor, and git config --global rebase.autoSquash true turns it on for every interactive rebase.
During any rebase, two commits can change the same lines and Git stops with a conflict. Fix it the same way as a merge conflict (see resolve Git merge conflicts), then git add the file and run git rebase --continue.
Check it worked
git log --oneline main..
sh greet.sh Ada
sh test.shThe log is back to three commits, with no fixup! in it. sh greet.sh Ada prints Hello, Ada, and the test still prints PASS.
Step 6 · Keep a stacked branch in step with --update-refs
Large changes often go out as stacked pull requests: a second branch built on top of the first, reviewed separately. Start a second branch from feature/greeting and add a feature:
git switch -c feature/greeting-shout
cat > greet.sh <<'EOF'
#!/bin/sh
name="${1:-world}"
msg="Hello, $name"
if [ "${SHOUT:-}" = "1" ]; then
msg=$(echo "$msg" | tr '[:lower:]' '[:upper:]')
fi
echo "$msg"
EOF
git commit -am "Add a SHOUT option"
SHOUT=1 sh greet.sh AdaThe last command prints HELLO, ADA. Now a reviewer of the first pull request asks for the README to mention the name argument. You are on the top branch, and the fix belongs in a commit on the lower one. Commit it as a fixup, then rebase the whole stack with --update-refs:
echo 'Pass a name to greet someone else: `sh greet.sh Ada`.' >> README.md
git commit -a --fixup=":/Document how to run greet"
git rebase -i --autosquash --update-refs mainThe todo list has a new kind of line:
pick 1e2f3a4 Add a greet script that says hello
pick 2f3a4b5 Document how to run greet
fixup 3a4b5c6 fixup! Document how to run greet
pick 4b5c6d7 Add a test for greet
update-ref refs/heads/feature/greeting
pick 5c6d7e8 Add a SHOUT optionupdate-ref tells Git to move feature/greeting to the rewritten commit at that point. Save and close. Without --update-refs, the rebase would rewrite the lower commits on your top branch only, and feature/greeting would still point at the old ones, so its pull request would never get the fix. Set git config --global rebase.updateRefs true if you stack branches often.
Check it worked
git log --oneline --decorate main..
git show feature/greeting:README.mdThe log shows four commits, with (feature/greeting) next to Add a test for greet and (HEAD -> feature/greeting-shout) on Add a SHOUT option. The README on feature/greeting ends with the Pass a name line.
Step 7 · Recover from a bad rebase with the reflog
Sooner or later you will delete the wrong line in a todo list. Make that mistake on purpose: start a rebase and delete the Add a test for greet line before saving.
git rebase -i main
git log --oneline main..
lsThe rebase succeeds, the log shows three commits, and test.sh is gone. The old commits still exist. Each branch keeps its own reflog, a list of where it has pointed, and a finished rebase is one entry:
git reflog show -n 3 feature/greeting-shoutThe newest entry, feature/greeting-shout@{0}, is something like rebase (finish): refs/heads/feature/greeting-shout onto ... from the bad rebase. @{1} is where the branch pointed before it. Warning: git reset --hard discards uncommitted changes, so run git status first and commit or stash anything you want to keep. Then move the branch back:
git status --short
git reset --hard "feature/greeting-shout@{1}"Right after a rebase, git reset --hard ORIG_HEAD also works, because Git saves the pre-rebase position there. ORIG_HEAD changes with the next reset or merge, though, while the branch reflog keeps entries for weeks: 90 days by default for commits still on a branch, 30 days for ones like these that no branch contains any more, after which git gc can delete them. The reflog is local to your machine, so recovery only works in the repository where the rebase happened. Undo mistakes in Git covers the reflog in more depth.
Check it worked
git log --oneline main..
sh test.shThe four commits are back, including Add a test for greet, and the test prints PASS.
Step 8 · Push rewritten history safely with --force-with-lease
Once a branch is on the server, rewriting it means the server's copy and yours no longer share history, and a normal push is rejected. To practise without GitHub, make a local bare repository to stand in for the remote and push the top branch to it:
git init --bare ~/git-demo/rebase-remote.git
git remote add origin ~/git-demo/rebase-remote.git
git push -u origin feature/greeting-shoutNow rewrite the top commit's message, as you might after a review comment, and try a normal push:
git commit --amend -m "Add a SHOUT environment variable"
git push || echo "Push rejected: the remote has a commit your branch no longer contains"Git refuses with ! [rejected] ... (non-fast-forward) or similar. Warning: the next command overwrites the branch on the remote. Use it only on a branch that is yours alone, never on main or a branch others have pulled, because their copies would no longer match and they would have to repair them by hand.
git push --force-with-lease--force-with-lease only overwrites the remote branch if it still points where your last fetch saw it. If a teammate pushed a commit you have not fetched, the push is refused instead of deleting their work: fetch, look at what they added, and bring it into your branch before trying again. Plain --force has no such check. If you run git fetch often, add --force-if-includes as well, which also refuses when the remote has commits you fetched but never incorporated.
Check it worked
git status -sb
git log --oneline -1 origin/feature/greeting-shoutThe first line of the status is ## feature/greeting-shout...origin/feature/greeting-shout with no ahead or behind, and the remote's newest commit is Add a SHOUT environment variable.
Step 9 · Clean up
Delete the practice repository and its stand-in remote:
cd ~
rm -rf ~/git-demo/rebase ~/git-demo/rebase-remote.gitThe Git PPA stays configured and keeps Git up to date. To stop using it, run sudo add-apt-repository --remove ppa:git-core/ppa. The installed Git stays in place until a newer Ubuntu package replaces it.
Check it worked
ls ~/git-demo 2>/dev/null || echo "no demo folder left"Neither rebase nor rebase-remote.git is listed. Anything else you keep in ~/git-demo is untouched.
Next steps
- Push a branch to GitHub and open the pull request: push to GitHub with SSH and open a pull request.
- Go deeper on recovery: undo mistakes in Git.
- Handle the conflicts a rebase can stop on: resolve Git merge conflicts.
Related tutorials
- How to Undo Mistakes in Git: restore, revert, reset and reflogYou will discard edits, unstage files, fix the last commit, undo a commit safely with revert, move a branch back with reset, and bring back commits you thought you had lost with the reflog.
- How to Create, Switch and Merge Branches in GitYou will create branches, switch between them, commit on each, merge one into another in the two ways Git can, read the branch graph, and delete branches you no longer need.
- How to Push to GitHub over SSH and Open a Pull RequestYou will connect a local repository to a new GitHub repository over SSH, push your work, push a branch, open a pull request from the website and from the GitHub CLI, merge it, and bring the result back to your computer.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials