Skip to content

This notebook belongs to

Sadat Jubayer

Senior Software Engineer at Premio

If found, please return to hello@jubayer.dev

Vol. 7Seven years of building

Started
January 2020
Everyday tools
TypeScript, Vue, React, Next.js, NestJS, React Native
Education
B.Sc. CSSE, AIUB, 2020
2 min readGit

Make fixup commits squash automatically

Set rebase.autoSquash so interactive rebase moves fixup commits beside their targets, then inspect the order before saving.

You made one commit for a form and one for its styles. After a review you made a third commit to fix the input label. You want that change in the form commit, without moving lines around by hand each time you rebase.

Mark the fix for its target

Say the form commit is Add form and the commit above it is Style form. Stage the label fix. Pass the hash of Add form to git commit --fixup. For this example, HEAD~1 names that commit because the style commit is at the tip.

git add form.html
git commit --fixup HEAD~1
git log -3 --oneline

The log shows a new fixup! Add form commit above Style form and Add form. Git derived the fixup subject from the commit you named.

Let interactive rebase reorder the todo list

Set rebase.autoSquash and start an interactive rebase from the parent of Add form. In the example, that parent is HEAD~3 after you commit the fixup.

git config --local rebase.autoSquash true
git rebase -i HEAD~3

The editor opens with Add form first, fixup! Add form next with the action fixup, and Style form last.

diagram

How autosquash reorders the todo list

Interactive rebase moves the label fix beside Add form before replaying Style form.

Save the todo list and close the editor. Git folds the fix into the form commit, keeps the form commit’s message, then replays the style commit on top. Run git log -2 --oneline and you see Style form above the rewritten Add form.

Read the todo list before saving

Check that the range contains the fixup and its target. If the todo list starts at Style form, extend the range to include Add form. When several commits share a subject, inspect which one Git paired with the fixup before letting it replay anything.

The examples use -i so you can check the order before Git starts replaying commits. Use git rebase -i --no-autosquash HEAD~3 on the original three-commit history if you want to inspect the todo list in its original order. If Git stops on a conflict, resolve it and run git rebase --continue, or use git rebase --abort to restore the branch to its starting point. Practise on commits you own because the rebase rewrites their history.