I don’t know how it is in your company, but in mine it’s considered a good practice to add ticket numbers to commit messages. It allows to easily determine why something was changed, etc. Makes sense, but this also means, that I should be adding this ticket to every message… And this doesn’t make sense for me. I will accidentally avoid it from time to time or make a lot of typos.

I prepared little automation to handle that. I use two git aliases. One reads ticket Id from git branch, the other automatically pre-fixes all my commit messages with it. If there’s no ticket, commit will fail.

Branch name might start with feature/, bugfix/ or hotfix/. Then comes ticket number, I guess your tickets also have format that can be easily matched with a regular expressions. Then, I like to describe branch purpose.

I have such aliases in my ~/.gitconfig:

# my custom aliases
    jira = !"f() { git rev-parse --abbrev-ref HEAD | sed -n -E 's#^(feature|(bug|hot)-?(fix)?)/([A-Z]+-[0-9]+)[^a-zA-Z0-9].*#\\4#p' ; }; f"
    cm = !"f() { if echo \"$1\" | egrep -q '^[A-Z]+-[0-9]+ '; then git add -A && git commit -m \"$1\"; else JIRA=$(git jira); if [ -z "$JIRA" ]; then echo >&2 '#### Start message with Jira ticket number! ####'; exit 1; else git add -A && git commit -m \"$JIRA $1\"; fi; fi; }; f"

How to use it?

Whether I use git-flow or just simple feature branching, before any commit, I always start with fresh branch:

Create new branch
git checkout -b feature/ABC-123-descriptive-branch-purpose

My aliases read ticket from branch, so I have to provide ticket number only once - during branch creation. Then, I commit my changes like this:

Commit changes
git cm "Descriptive message"

Result of it is:

What it does
git add -A
git commit -m "ABC-123 Descriptive message"

With this simple trick, I have to pay attention to ticket number once in a while. Rest is magic 😎