Sub topic 11
Working faster with --fixup and --autosquash
This chapter assumes you can run git rebase -i, read the todo list, and know what fixup does. You should also be comfortable with git commit, git log, and remote branches such as origin/main.
The problem: manual todo editing
Suppose you find a bug in a commit from three commits ago. With a plain interactive rebase, you would commit the fix, open the todo list, move the fix line under its target, and change pick to fixup. Git can do that editing for you.
Creating a fixup commit
git commit --fixup <commit> creates a new commit. Its message is fixup! followed by the subject of the target commit. Git uses that prefix later to match the two commits.
git log --oneline -n 5
git add src/login.js
git commit --fixup 3f2a1b9The log now shows something like this:
e91c4d0 fixup! Add login form
a6b2c33 Add login tests
51ab9c0 Add validation
8c4d7e2 Fix typo in login form
3f2a1b9 Add login formThe fixup commit sits at the tip of the branch. Nothing has been rewritten yet. It is an ordinary commit that you can still push to a private branch or change.
Applying fixups with --autosquash
Run an interactive rebase with --autosquash. Git scans the todo list for commits whose subject starts with fixup! or squash!. It moves each one directly below its target and sets its command to fixup or squash.
git rebase -i --autosquash origin/mainThe todo list opens already arranged:
pick 3f2a1b9 Add login form
fixup e91c4d0 fixup! Add login form
pick 8c4d7e2 Fix typo in login form
pick 51ab9c0 Add validation
pick a6b2c33 Add login testsCheck the list, then save and close the editor. The fix is folded into Add login form, and the other commits are replayed on top as new commits.
Variations
git commit --squash <commit>creates asquash!commit. During the rebase, Git opens an editor so you can combine the two messages.git commit --fixup=reword:<commit>creates a commit that only changes the target's message. Use--fixup=amend:<commit>to change both the contents and the message. Both need Git 2.32 or later.- To avoid typing
--autosquasheach time, rungit config --global rebase.autoSquash true. It then applies to everygit rebase -i. - To accept the generated todo list without opening the editor, set
GIT_SEQUENCE_EDITOR=truefor that command:GIT_SEQUENCE_EDITOR=true git rebase -i --autosquash origin/main.
Common errors
- Choosing a base that excludes the target. The base must be older than the target commit. If it is not, the
fixup!commit has nothing to attach to and stays where it is. - Fixing up a commit that is already shared. The rebase replaces the target with a new commit. If others have based work on it, follow the rules in the chapter on pushing rewritten history.
- Staging too much.
--fixupcommits whatever is staged. Usegit add -pso that the fixup contains only the change for the target. - Conflicts. A fixup can conflict with commits between it and its target. Handle this as in any rebase: resolve, then
git rebase --continue.
Exercise
You made a fixup commit with git commit --fixup 3f2a1b9. You then run git rebase -i origin/main without --autosquash and with rebase.autoSquash unset. What does the todo list look like, and what do you need to do?
Show answer
The fixup! ... commit stays at the end of the list as a normal pick. Either move it under its target and change pick to fixup by hand, or abort and rerun with --autosquash, which does both steps for you.
Exercise
Which command makes the fixup arrangement happen on every interactive rebase, without a flag?
Show answer
git config --global rebase.autoSquash true