Here’s today’s instalment of Why Didn’t I Think of This Sooner™.

I’ve got into the habit of squashing commits before I push them as part of a pull request. In order to run the command I use to do this — git rebase -i HEAD~n — I need to know the number of commits I want to squash (this will be the value of n).

Fortunately, I’ve got into the habit of prefixing each commit message with the Jira task number. For example, I may write a commit message with the first line being ABC-123: fixed a bug in the thing, followed by a more detailed list of changes. I do for every commit, even for those “checkpoint commits” I make before changing branches.

So all I need to do to get the number of commits I need to squash is to simply count up all the commits that start with the Jira task number. For all this time, the way I do this is to run git log to open up the history in Vim, and manually count the number of commit messages that began with the particular Jira task number.

Today, I remembered that I could use a shell pipeline to do the same thing:

git log | grep ABC-123 | wc -l

which would save me stack-loads of time and potential mistakes.

Wish I thought of this sooner. 🤦