Hands-On: Clean Up a Messy History

Updated

October 30, 2026

Hands-On: Clean Up a Messy History

Make three noisy commits on a feature branch, then squash them into one conventional commit with interactive rebase.

 before                         after fixup + amend

 A ← wip ← more ← typo          A ← docs: mention a future health endpoint
     feat/health-header             feat/health-header

Setup

cd ~/desk/desk-api
git switch main
git switch -c feat/health-header

Three messy commits

echo "# health" >> README.md
git add README.md
git commit -m "wip"

echo "Health endpoint later." >> README.md
git add README.md
git commit -m "more"

echo "See src/ping.py for now." >> README.md
git add README.md
git commit -m "typo fix later"
git log --oneline -5

Squash with GNU sed (Linux)

Keep the first of the three; fixup the rest:

GIT_SEQUENCE_EDITOR="sed -i '2,\$s/^pick /fixup /'" git rebase -i HEAD~3
git log --oneline -5

One commit remains, message still wip. Retitle:

git commit --amend -m "docs: mention a future health endpoint"
git log --oneline -3

Optional: rebase onto main

If main moved, replay this one commit:

git rebase main
git log --oneline --graph --decorate -8

Cleanup

git switch main
git merge --ff-only feat/health-header
git branch -d feat/health-header

If --ff-only refuses, main moved; rebase the feature first or merge.

Done when

  • git log on main shows one docs: commit for this work, not three wips
  • git status is clean
  • The feature branch is deleted

Next chapter: copy a single commit onto another branch (cherry-pick) and undo a published commit (revert).