Fixing commits with git commit --fixup and git rebase --autosquash

The way I like to make changes in response to a PR review is to make any fixes to the original commits using a rebase. I don't want commits that "fix typos" in my history, especially at the feature branch level. There are varying opinions and approaches to this, but I think rebasing is great tool when used on your own branches and in moderation.

Standard procedure for this is - 1) Make the code change; 2) Commit the change; 3) Start an interactive rebase; 4) Identify the commit that needs fixing; 5) Move the new commit underneath it; 6) Change it to "squash". It's quite tedious.

Fixup commits

Fixup commits are created using git commit --fixup <SHA>. Practically, --fixup associates a new commit with an existing commit so that when you do an interactive rebase, you don't have to re-order any commits in order to squash them. And you don't have to change any commit messages. From the docs:

Construct a commit message for use with rebase --autosquash.


--autosquash is a flag to use with rebase and takes everything a step further. Once you've committed your changes with git commit --fixup <SHA> you can start an interactive rebase as normal, but pass the --autosquash flag too.

git rebase -i --autosquash master

Now you won't have to do anything, the rebase will automatically take care of squashing those commits created with --fixup in the correct order!

You can have this behaviour by default, which seems safe and sensible, by setting autosquash = true in your ~/.gitconfig.

  autosquash = true

Automating further

I use --fixup so much that I have a helper alias in my ~/.gitconfig.

  fixup = "!git log -n 50 --pretty=format:'%h %s' --no-merges | fzf | cut -c -7 | xargs -o git commit --fixup"

This lets me type git fixup and presents a list of my 50 most recent commits and allows me to search the list using fzf. Once a commit is selected, the SHA is passed to git commit --fixup.

It works like this:

I hope this makes your commit fixing easier!