Member-only story
How to Prevent Git Commit Naming Mistakes
Throw a safety net each time you commit with Git hooks
I’ve been using Git for many years, and I’m still far from knowing it all. However, I feel like Git has the answer each time I face a problem. More recently, I’ve been wondering how we could remove the boredom of prefixing commit messages with a Jira ticket number.
I’ve always felt that naming my branches and commit messages was tedious. Despite this fact, I always strive to respect naming conventions. They’re vital to keeping proper versioning and helping your teammates seek your branches.
So what are the options? You can still manually name your branches, but it’s tedious and error-prone. If you use Git flow, you may be tempted by a wrapper to drive you through the naming. Unfortunately, my experience with it wasn’t conclusive. Besides, you may want some flexibility in your naming, and I found this tool too rigid.
Rejoice! Git always has an ace up its sleeve. You can intercept specific Git commands before they’re performed to execute scripts. They’re called hooks.
I won’t describe them all. Instead, I’ll present two of them to:
- Enforce a branch naming policy.
- Prefix commit messages with the branch name ticket.
1. Enforcing Branch Naming Policy
I mentioned Git flow in my introduction. This branching convention is popular amongst developers. It results in prefixing your branches with names such as master
, develop
, feature
, bugfix
, and so on. When naming your branch, you may also want to append a ticket number.
At my company, we enforce this naming policy:
- Branches must start with one of the Git flow keywords followed by a splash.
- Then indicate the Jira project name and ticket number linked with a hyphen.
- Then the developer initials linked with an underscore.
- Finally, the description of the ticket.
One example would be:
feature/GTBC-9999_SV_git_hooks
A hook can check all those conditions. For instance, ensure that your branch name respects…