If you aren’t already, defining your project Git commit message convention is always on every developer’s “To-Do” list. Like flossing your teeth – everyone knows it’s necessary best practice for healthy gums and avoiding the dentist, but it ends up on the ‘I’ll move it to tomorrow’s to-do list’ aka, procrastination galore.
I’m going to break down the reasons why you really have NO excuse not to set a Git commit message convention (or floss), and the required steps in order to move this task from your “To-Do” list to “DONE” in a few simple steps!
I’ll leave your dentist to yell at you about not flossing.
It is important to communicate the nature of changes in projects in order to foster transparency to a slew of people: existing teammates, future contributors, and sometimes to the public and other stakeholders. It’s obvious why a well-formatted Git commit message convention is the best way to communicate context about a change to fellow developers (and their future selves) when requesting a peer code review. A commit message convention also makes it easier to explore a more structured commit history and to understand which notable changes have been made between each release (or version) of the project.
“$ git log” is a beautiful and useful snippet. A well-organized commit message history leads to more readable messages that are easy to follow when looking through the project history. Suddenly, navigating through the log output become a possible mission! Embracing a commit message convention will also help you properly use other git commands like git blame, git revert, git rebase, git shortlog and other git subcommands.
Automation, automation, automation. Once you know you can rely on a standardized Git commit message, you can start building a flow around it and leverage the power of automation to level-up your project development flow:
Now that we know that having a commit message convention is useful whether you’re working on an open source project, working on your own, or working with your team on a single project, standardizing the Git commit message is the only right way to commit!
We covered the “why” part and now we will move to the “how” part – in my opinion, there are pretty much only two ways to go:
This approach is a simple and easy guideline, good for getting used to the idea of having a convention/have a majority of coders or junior developers on the team. It’s the top 5 best practices to implement TODAY:
This approach is relevant for advanced/engaged teams, the key benefit of this approach is that you can also use the supporting tools in the ecosystem of the chosen conventions. There are plenty of different conventions so I will focus on the top two:
If you got this far, you probably agree with my opinion that every project should have a defined commit message convention. Now, the question is how can I make sure all the project committers (me, outside contributors and teammates) are aligned about the chosen convention and apply to it? My top two solutions for that are:
Git hooks are scripts in Git that can be triggered and executed automatically, before or after different Git events. The hooks are a built-in feature of Git and can run locally.
The pre-commit hook is the relevant Git hook to set in order to enforce commit message conversion.
ProsConsA built-in Git feature, which means it can be used with every Git vendor (GitHub/GitLab/BitBucket) “out-of-the-box”No “gate-keeper” since requirements are defined by the user, meaning they can also be easily ignored by the userCan be triggered locally before every commit so the feedback loop is immediateConfigured locally so every existing and potential contributor needs to install it on their own environment
One of Datree’s services is a policy enforcement solution for compliant code and setting a custom Git commit message is one of the policies we support.
ProsConsEasily defined in one place so there is no need to distribute the regex across every developer and configure it locallyBased on GitHub’s ‘checks new API’, which means it’s supported only by GitHubEasily enforced from the server side so it’s harder to push non-compliant commit messages to the master branchThe check is automatically triggered by the pull request. At this stage, you get an indication if all the commits in the pull request are following correct commit message convention
MethodBest PracticeRegExAdopt defacto best practicesHave a commit message^[^\s]{1,3}.*$Keep a short subject line^.{1,50}$Don’t end the subject line with a period.*[^.]$Start with a capital letter^[A-Z].+Link to a planning system – Jira((?!([A-Z0-9a-z]{1,10})-?$)[A-Z]{1}[A-Z0-9]+-\d+).+Adopt an existing conventions frameworkAngular Git commit message guidelines^(feat|fix|docs|style|refactor|test|build|ci|perf)(\(.+\))?\:\s(.{3,})Emoji-Log commit message convention^(\:package:\ NEW|\:ok_hand\: IMPROVE|\:bug\: FIX|\:rocket\: RELEASE|\:book\: DOC|\:white_check_mark\: TEST)(\(.+\)\:\s)?\:\s(.{3,})
It’s imperative to set up a policy that everyone adheres to for commit messaging and floss your teeth. While both may seem like a pain now, they aren’t a big deal, once you start, you wonder why you waited so long and having healthy gums and compliant code both make you smile more.