I recently happened upon an article by Julia Evans on what can go wrong when rebasing in Git. This made me realise that I should probably talk about my favourite, yet obscure, Git feature.

When using commit you can use --fixup <commitid> or --squash <commitid> to create a commit that can be automatically fixup’d or squashed on the next rebase with --autosquash. This is handy, but you need to know the commitid beforehand.

There is a type of refspec that can resolve a regular expression to the commitid of a matching commit: :/<RegExp>. This will find the ID of the most recent commit (not necessarily on your current branch) with message matching /<RegExp>/, and resolve to that.

It’s a killer feature with --fixup and --squash: in a pinch, you can create fixes to past commits that

  1. you only vaguely remember the message of, and
  2. Git can automatically move (autosquash) in the next interactive rebase.

For example git commit --fixup ":/hat feat" may create a !fixup That feature commit. The !fixup prefix is what --autosquash uses to reorder commits.

Here’s a quick video example.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.