Fixup Commits and Autosquash
A fixup commit is a commit that corrects an earlier commit on the same branch, and says so in its message. You create it with git commit --fixup=<commit>: Git writes the message fixup! <subject of that commit>. Later, git rebase --autosquash finds every such commit, moves it under the commit it fixes, and folds it in. It is used to correct older commits, for example after review comments, without opening the interactive rebase list and moving lines by hand.
Think of sticky notes on a printed report. While reading it, you put a note on page 3: "fix the total here". You keep reading and add more notes. At the end, you go through the notes once and make every correction on the page it belongs to. The notes tell you where each fix goes, so you don't have to remember.
Syntax
git commit --fixup=<commit>
git rebase -i --autosquash <base>
git rebase --autosquash <base>| Command or option | Description |
|---|---|
git commit --fixup=<commit> | It is used to commit the staged changes as fixup! <subject>, to be folded into <commit> with its message kept. |
git commit --squash=<commit> | It is used to commit as squash! <subject>, so the messages are combined when folding. |
git commit --fixup=amend:<commit> | It is used to create an amend! commit that also replaces the target's message. |
git commit --fixup=reword:<commit> | It is used to replace only the target's message, with no content change. |
git rebase -i --autosquash <base> | It is used to open the to-do list with the fixups already moved and marked. |
git rebase --autosquash <base> | It is used to apply the fixups directly, without opening the list. |
git config --global rebase.autoSquash true | It is used to make every interactive rebase autosquash. |
The Starting Point
Navika's receipts branch has three commits, and is up for review:
mkdir fee-tracker
cd fee-tracker
git init --quiet
echo "Fee Tracker" > README.md
git add README.md
git commit --quiet -m "Add the README"
git switch --quiet -c feature/receipts
echo "Receipt No. 1" > receipts.txt
git add receipts.txt
git commit --quiet -m "Add receipt numbers"
echo "class Receipt {}" > Receipt.java
git add Receipt.java
git commit --quiet -m "Add the Receipt class"
echo "Receipts are printed after payment." >> README.md
git commit --quiet -am "Describe receipts in the README"
git log --oneline main..6770956 (HEAD -> feature/receipts) Describe receipts in the README
c3ca53e Add the Receipt class
e5d493c Add receipt numbersExample 1: Fix an Older Commit
Advitha reviews it: receipt numbers should start at 1001, not 1. The mistake is in the first commit, two commits back. Navika makes the correction and commits it as a fixup for that commit. She can name the commit by its ID, or with :/ and part of its message (see "Naming Commits: HEAD, ~ and ^"):
echo "Receipt No. 1001" > receipts.txt
git commit --fixup=":/Add receipt numbers" -a
git log --oneline main..[feature/receipts 45bd592] fixup! Add receipt numbers
1 file changed, 1 insertion(+), 1 deletion(-)
45bd592 (HEAD -> feature/receipts) fixup! Add receipt numbers
6770956 Describe receipts in the README
c3ca53e Add the Receipt class
e5d493c Add receipt numbersExplanation: Git wrote the message itself: fixup! Add receipt numbers. The fix is an ordinary commit for now, at the top of the branch. Nothing has been rewritten yet. Navika can push it as it is, so the reviewer sees exactly what changed in response to the comment.
Example 2: Apply the Fixups with --autosquash
When the review is done, --autosquash cleans up. With -i, Git opens the to-do list with the fixup already moved under its target and marked fixup. On this page, GIT_SEQUENCE_EDITOR=cat prints that list and accepts it unchanged:
GIT_SEQUENCE_EDITOR=cat git rebase -i --autosquash main | head -4pick e5d493c # Add receipt numbers
fixup 45bd592 # fixup! Add receipt numbers
pick c3ca53e # Add the Receipt class
pick 6770956 # Describe receipts in the README
Successfully rebased and updated refs/heads/feature/receipts.git log --oneline main..
cat receipts.txt4e44c72 (HEAD -> feature/receipts) Describe receipts in the README
7a9ea82 Add the Receipt class
ad4e36e Add receipt numbers
Receipt No. 1001Explanation: Git prepared the list by itself: the fixup! commit moved from the bottom to just below "Add receipt numbers", with the command fixup. Accepting the list folded the correction into that commit. Three commits remain, and the first one now creates receipts.txt with 1001 from the start. (| head -4 only shortens the printed list by leaving out the help text.)
Example 3: Several Fixups, No Editor
Review continues with two more comments: the class needs a field, and the README should say "after each payment". That means two fixups, for two different commits:
echo "class Receipt { int number; }" > Receipt.java
git commit --quiet --fixup=":/Add the Receipt class" -a
sed -i.bak "s/after payment/after each payment/" README.md
rm README.md.bak
git commit --quiet --fixup=":/Describe receipts" -a
git log --oneline main..db0b582 (HEAD -> feature/receipts) fixup! Describe receipts in the README
3c4426d fixup! Add the Receipt class
4e44c72 Describe receipts in the README
7a9ea82 Add the Receipt class
ad4e36e Add receipt numbersSince Git 2.44, --autosquash also works without -i. Git applies the fixups straight away, without opening the list:
git rebase --autosquash mainSuccessfully rebased and updated refs/heads/feature/receipts.git log --oneline main..1aee272 (HEAD -> feature/receipts) Describe receipts in the README
fa1acdb Add the Receipt class
ad4e36e Add receipt numbersBoth fixups were folded into their commits. Setting rebase.autoSquash to true makes git rebase -i autosquash every time, so you don't have to type the option.
--fixup vs --squash
git commit --fixup=<c> | git commit --squash=<c> | |
|---|---|---|
| Message created | fixup! <subject> | squash! <subject> plus anything you write |
| When folded | The target keeps its message | You edit the combined messages |
| Use it for | Corrections that need no explanation | Changes whose explanation should join the target's message |
Common Mistakes
Autosquashing too early. The reviewer can no longer see what changed since the last review. Fold the fixups in at the end.
Changing the target's subject before autosquashing. The fixup's fixup! <old subject> no longer matches. Autosquash also matches by commit ID if you created the fixup with an ID, but the safest order is to fix messages last.
Forgetting to stage the correction. git commit --fixup commits what is staged, like any commit. Add -a or stage first.
Fixups for commits already on main. Autosquash rewrites the branch's own commits only. A mistake that is already merged is fixed with a normal new commit.
Leaving fixup! commits in a merged branch. They clutter main's history. Autosquash before merging, or let the platform squash.
Under the Hood
A fixup commit is an ordinary commit. The only thing special about it is its message: fixup! , squash! or amend! followed by the subject (or ID) of the target. When --autosquash builds the to-do list, Git scans it for such messages, moves each one directly below the commit whose subject (or ID) matches, and replaces its pick with fixup or squash. The rest is a normal interactive rebase. That is why you can also write such a message by hand, and why fixups work in any tool that runs git rebase.
Interview Corner
What is a fixup commit?
A commit made with git commit --fixup=<commit>, whose message is fixup! <subject of commit>. It holds a correction to an earlier commit, and is folded into it later by git rebase --autosquash.
What does --autosquash do?
During a rebase, it finds fixup!, squash! and amend! commits, moves each under the commit it targets, and marks it to be folded in. With -i, you see the prepared list. Without -i (Git 2.44 and later), the fixups are applied directly.
What is the difference between --fixup and --squash?
When folded in, a --fixup commit's message is discarded and the target's message kept. A --squash commit's message is combined with the target's, and you edit the result.
How do you respond to review comments on older commits of a pull request?
Commit each correction with git commit --fixup=<commit> and push, so reviewers see the changes. Before merging, run git rebase -i --autosquash main and push with --force-with-lease.
Practice
Challenge: one more round. Advitha asks for one last change: receipt numbers should start at 2001. Make it a fixup for "Add receipt numbers", autosquash without -i, and print the branch's commits and receipts.txt.
What does Git print? Before you open the solution, decide how many commits the branch has after the autosquash, and whether any fixup! message remains.
Show solution
echo "Receipt No. 2001" > receipts.txt
git commit --quiet --fixup=":/Add receipt numbers" -a
git rebase --autosquash mainSuccessfully rebased and updated refs/heads/feature/receipts.git log --oneline main..
cat receipts.txt8067042 (HEAD -> feature/receipts) Describe receipts in the README
1d035d7 Add the Receipt class
4b7d077 Add receipt numbers
Receipt No. 2001Still three commits, with no fixup! left: the correction was folded into "Add receipt numbers", which now creates the file with 2001.
Checkpoint. In a practice repository with a four-commit branch, make three fixup commits for three different commits, in a mixed order. Run git rebase -i --autosquash and check in the list that each fixup was placed under the right commit before you accept it.
Key Takeaways
Quick reference
| Command | It is used to |
|---|---|
git commit --fixup=<commit> -a | Record a fixup for an older commit |
git commit --fixup=":/<text>" | Name the target by its message |
git rebase -i --autosquash main | Review and apply the fixups |
git rebase --autosquash main | Apply the fixups without the list |
git config --global rebase.autoSquash true | Always autosquash in rebase -i |
Was this tutorial helpful?