To generate release notes automatically with Git, many teams use conventional commits. Often, you use a platform like GitHub or Azure DevOps to track features and bugs.
When it comes to commit messages, including important information in this small string (ticket ID, scope, etc.) requires the whole team to be on the same page to keep the release notes clean.
Let’s see how you can enforce a commit message policy.
Enforcing Git Commit Messages On Azure DevOps Repositories
Azure Repos doesn’t offer custom server-side Git hooks, so you can’t reject a push because of its commit messages. Its push policies only cover things like author email, file paths, reserved names, path length and file size.
So how do you make the whole team commit with consistent messages?
You can use a commit-msg hook on developers’ local machines. For that, create a .git/hooks/commit-msg file in each developer’s local repository.
This validates messages before they’re committed locally.
For example, the following commit-msg hook ensures the subject line follows the Conventional Commits structure: <type>[optional scope][!]: <description>.
| |
The specification only requires feat and fix. The other types (docs, perf, ci…) come from the Angular convention, so adapt the list to your team. The ! marks a breaking change.
Custom Enforcement Rules
If your team doesn’t follow conventional commit rules, you can tweak the regular expression:
| |
Anything that doesn’t match the pattern will fail the git commit -m command.
Note that this #[work_item_id] format only works with git commit -m (or -F). When you write the message in the editor, Git’s default cleanup removes the lines starting with #: the hook accepts the message, then Git aborts with “Aborting commit due to empty commit message.” If your team commits from the editor, set git config commit.cleanup whitespace to keep the # lines.
Keep in mind that a local hook is a safety net, not a lock: git commit --no-verify skips it.
Alternatives on the Azure DevOps Side
Since the server can’t reject a bad message, you can still check it after the push:
- Build validation: add a build validation branch policy that runs a pipeline script checking the commit messages of the pull request.
- Pull request status check: a custom service can post a status on the pull request that blocks the completion until the messages are valid.
Both flag the problem at PR time rather than at commit time, but nobody can skip them with --no-verify.
Ensuring the Rule is Applied For All
Since .git/hooks isn’t tracked by Git, you have an option that requires a single step to make sure the commit message rule is enforced:
- create a directory
.githooks/at the root of the repository. - create the
commit-msghook with the content above and make it executable (chmod +x .githooks/commit-msg) - update the
README.mdto communicate the fact that everyone needs to run this command when cloning the repository:
| |
Another idea would be to include an initial-repo-setup.sh script to run once after cloning the repository:
| |
Conclusion
On a project with several people working together, enforcing a good strategy to use consistent commit messages is a best practice, but it needs to be discussed, because opinions often vary from one person to another.
Follow me
Thanks for reading this article. Make sure to follow me on X, subscribe to my Substack publication and bookmark my blog to read more in the future.
Credit: Git Logo by Jason Long is licensed under the Creative Commons Attribution 3.0 Unported License.