brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: git commit --fixup plus --autosquash keeps your history clean without any editing

Git

You spot a typo in a commit from ten minutes ago, and the lazy move is a new commit called “oops” or “fix”. Do that a few times and your branch reads like a diary of mistakes. Git has a built-in way to fix the old commit instead, and it takes one extra word.

What it does

git commit --fixup makes a commit that is labelled as a correction of an earlier commit. Later, git rebase --autosquash reads those labels, moves each correction directly under the commit it belongs to and folds them together. The result is a branch where every commit is one clean idea, and you never had to open an editor and drag lines around by hand.

I like it because the moment you notice the mistake is the moment you know which commit it belongs to. Postponing that decision to the end of the branch means you have to work it out again from memory.

The two steps

Find the commit you want to correct, change the file, stage it and commit against that hash:

git log --oneline
git add -p
git commit --fixup a1b2c3d

Git creates a commit with the message fixup! <the original message>. Repeat as often as you like, for different commits. When the branch is ready, rebase onto the branch you started from:

git rebase -i --autosquash origin/main

The editor opens with the todo list already sorted: each fixup line sits under its target commit. Save and close, and Git does the rest. If nothing conflicts, you are done in a few seconds.

Variations

  • git commit --fixup=reword:a1b2c3d lets you change only the commit message of the old commit (Git 2.32 and newer).
  • git commit --squash a1b2c3d works the same way but lets you edit the combined message during the rebase.
  • git config --global rebase.autoSquash true turns on autosquash for every interactive rebase, so you can leave the flag off.

To fix the very last commit you do not need any of this: git commit --amend is enough.

Caveats

A rebase rewrites history. Do it on your own branch before anyone else pulled it, and push afterwards with git push --force-with-lease, which refuses to overwrite work you have not seen. Never autosquash a shared branch such as main. If it goes wrong, git rebase --abort brings you back, and git reflog remembers where the branch was before.

An old commit that already changed the same lines as your fixup can cause a conflict during the rebase. That is normal. Fix it, run git add and git rebase --continue.

Further reading

More Git tips git show-branch main feature git log --stat -5 git merge --no-ff feature -m "Merge feature" git merge --squash feature git merge --abort All Git tips (144) All topics

Link