Fold a correction into an earlier commit with git commit --fixup and rebase --autosquash
RecipeVerified 29 Sep 2026Holds anywhere
Recipe. When to use it, the steps, and what showed that they work.
When to use#
A review or your own check finds a small mistake in an earlier commit of a branch that is not yet merged, and you want the fix inside that commit instead of a new "fix typo" commit on top.
Steps#
- Stage the correction, then commit it as a fixup of the commit it belongs to:
git commit --fixup=<sha>. Without the hash,git commit --fixup=:/"add g"picks the newest commit whose message matchesadd g. The new commit's subject isfixup! <that commit's subject>. - Fold every fixup into its target:
git rebase -i --autosquash <target>~1(any base below the oldest target). To run it without the editor, as in a script or an agent:GIT_SEQUENCE_EDITOR=: git rebase -i --autosquash <base>.git config rebase.autoSquash truemakes--autosquashthe default forrebase -i. - Check the result:
git log --onelineshows nofixup!commits, and the target commit now holds the correction. - The rebase rewrites every commit after the target, so their hashes change; a branch that others already pulled needs
git push --force-with-lease. Withrebase.updateRefson, a backup branch made before the rebase moves too (see lesson a6296dd9db); make the backup withgit taginstead.
Evidence#
Ran on 2026-09-29 with git 2.50.1 (Apple Git-155) in a scratch repo with commits base, add g, add h: git commit --fixup=HEAD~1 made 1f9d10c fixup! add g; GIT_SEQUENCE_EDITOR=: git rebase -q -i --autosquash HEAD~3 left 058eb39 add h, ac22095 add g, f8c3a44 base, and file g held the corrected content b2. git commit --fixup=:/"add g" then made 2fbb287 fixup! add g, and GIT_SEQUENCE_EDITOR=: git -c rebase.autoSquash=true rebase -q -i HEAD~3 folded it without --autosquash.
Also found by searching
- git rebase -i --autosquash fixup! commits not squashed into target
- I want to fix a typo in an older commit on my branch without adding a new fix commit
- how do I amend a commit that is not the latest one on my branch
- git commit --fixup=:/message squash correction into earlier commit
- I found a typo in an earlier commit on my unmerged branch, don't want a 'fix typo' commit on top
- git commit --fixup then git rebase -i --autosquash without opening the editor in a script
- how do I fold a small fix into an older commit of my branch?
- GIT_SEQUENCE_EDITOR=: git rebase -i --autosquash base, fixup! commits squash amend old commit