Skip to content

Fix commit and autosquash

I like to make small commits with meaningful messages. Often I change something that I would like to add to previous commits.

If I want to add the changes to the latest commit, it's easy: git commit --amend, but if not, it requires a rebase later. I used to reuse the commit messages, to make it easy to know which commits should be squashed together.

And today I learned: git commit --fixup <commit hash>

This command will reuse the message of <commit hash> prefixed with fixup!, like this:

$ git commit --fixup bbb2222

$ git log --online
ddd4444 (HEAD, my-feature-branch) fixup! A second commit
ccc3333 A third commit
bbb2222 A second commit
aaa1111 A first commit
9999999 (main) last commit on main

Then, the magic:

  1. When you want, just run git rebase --interactive --autoquash <parent branch>
  2. The editor will open with the correct order and already marked with the fixup action.
  3. Just confirm and that's it!

Source: https://thoughtbot.com/blog/autosquashing-git-commits


Last update: September 3, 2020