I learned something new from Julia Evans today.

Git has a --fixup argument when committing files which makes it easy to create fixup commits which can get automatically squashed when rebasing. This is fantastic!

I’ve known about git commit --amend for years, but this allows you to fixup commits which are several commits back without manually moving a bunch of lines around while interactively rebasing…

Let’s assume you have a repo which looks like shown below. 696792e9 is our “bad” commit, which we need to fixup.

$ git log --oneline
03dd314c (HEAD -> main) improve some stuff
696792e9 bad commit  <------------------------------ our bad commit
647d4c72 initial commit

You can add a fixup commit like so:

$ git com --fixup 696792e9
[main a88a4f8c] fixup! bad commit
 1 file changed, 1 insertion(+), 1 deletion(-)

This added the commit with a description of fixup! bad commit. Our history now looks like:

$ git log --oneline
a88a4f8c (HEAD -> main) fixup! bad commit  <-------- fixes our bad commit
03dd314c improve some stuff
696792e9 bad commit
647d4c72 initial commit

When we interactively rebase with the --autosquash argument (e.g., git rebase --interactive --autosqaush main), this will automatically change commit a88a4f8c into a fixup commit and place it in the proper order in the list of commits:

pick 696792e9 bad commit
fixup a88a4f8c fixup! bad commit  <----------------- this happened automatically!
pick 03dd314c improve some stuff

Brilliant!

You can enable --autosquash by default with: git config --global rebase.autosquash true