Sub topic 10
Fixup commits and --autosquash
This chapter assumes you can read git log output, know what origin/main is, and have run git rebase -i at least once. Here you will learn how to mark a correction as belonging to an earlier commit, and how to have Git fold it in for you.
The problem
You are on a feature branch with several commits. During review you notice a typo in a commit made three commits ago. You could fix it in a new commit named "Fix typo", but that clutters the history. You could start an interactive rebase and mark the commit with edit, but that takes many manual steps.
A fixup commit solves this. It is an ordinary commit whose message tells Git which earlier commit it corrects. Later, one rebase command moves it into place and merges it with its target.
Creating a fixup commit
Stage your correction, then commit it with --fixup and the hash of the commit you want to amend:
git commit --fixup a1b2c3dGit creates a commit with the message fixup! Add login form. That subject line is a copy of the target commit's subject, prefixed with fixup!. The prefix is the marker that --autosquash looks for.
Applying it with --autosquash
Run an interactive rebase with --autosquash:
git rebase -i --autosquash origin/mainGit builds the todo list as usual, then makes two changes before showing it to you:
- It moves each
fixup!commit directly below the commit it targets. - It changes that commit's command from
picktofixup.
The list looks like this:
pick a1b2c3d Add login form
fixup 9f8e7d6 fixup! Add login form
pick 1234567 Add session handlingSave and close the editor. Git replays the commits, and the fixup line is folded into the commit above it. The fixup's message is discarded and the target keeps its original message.
Related variants
git commit --squash <hash>creates asquash!commit. With--autosquashit becomes asquashline, so Git pauses and lets you combine the two commit messages.git commit --fixup=reword:<hash>creates a commit that replaces the target's message when autosquashed.git commit --fixup=amend:<hash>replaces both the target's content and its message.
Making it the default
If you always want this behavior, set it once:
git config --global rebase.autoSquash trueAfter that, git rebase -i applies autosquash without the flag.
Common errors
- Nothing moves.
--autosquashonly works with-i. Without-i, the flag has no visible effect unless you enable the config option and still run an interactive rebase. - Wrong target. If the hash you gave belongs to a commit outside the range being rebased, the fixup cannot find a match. It stays at the bottom as a plain
pick. Use a base that is older than the target commit. - Rewritten hashes. Autosquash is a rebase, so every commit after the target gets a new hash. Do not do this to commits others already have. If you have already pushed, you will need
git push --force-with-lease, covered in an earlier chapter. - Conflicts. Folding a fixup into its target can conflict with commits in between. Resolve it as you would any rebase conflict, then run
git rebase --continue.
Exercise
You committed a typo fix with git commit --fixup a1b2c3d. What happens when you run git rebase -i --autosquash origin/main, and why must the base be older than a1b2c3d?
Show answer
Git moves the fixup! commit directly below a1b2c3d in the todo list and changes its command to fixup. When the rebase runs, the fix is merged into a1b2c3d and the fixup's message is dropped. The base must be older than a1b2c3d so the target is in the rebased range. Otherwise Git cannot find it, and the fixup stays as a separate commit.
Exercise
What is the difference between a fixup! commit and a squash! commit when autosquashed?
Show answer
A fixup! commit is merged into its target and its message is discarded. A squash! commit is also merged, but Git stops so you can edit the combined commit message.