Logo of Git Versioning Software

Enforcing Commit Message Standards in Azure DevOps

Commit messages convey important information, often used to provide the scope of change between versions.

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>.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#!/bin/sh
subject=$(head -n 1 "$1")
pattern='^(feat|fix|build|chore|ci|docs|style|refactor|perf|test|revert)(\([a-z0-9._/-]+\))?!?: .+$'

# Let the messages generated by Git itself through
case "$subject" in
    Merge\ *|Revert\ \"*) exit 0 ;;
esac

if ! echo "$subject" | grep -qE "$pattern"; then
    echo "Error: Commit message doesn't follow conventional commit format"
    echo "Format: <type>[optional scope][!]: <description>"
    exit 1
fi

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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/bin/sh

commit_msg=$(cat "$1")

# Pattern: #[work_item_id]-commit-type: [message <= 34 chars]
# Examples:
#   #1234-feat: add user login
#   #5678-fix(auth): resolve token issue
pattern="^#[0-9]+-[a-z]+(\\([a-zA-Z0-9_-]+\\))?: .{1,34}$"

if ! echo "$commit_msg" | head -n 1 | grep -qE "$pattern"; then
    echo "❌ Error: Commit message doesn't follow the required format"
    echo ""
    echo "Required format:"
    echo "  #[work_item_id]-commit-type: [message]"
    echo ""
    echo "Rules:"
    echo "  - Must start with # followed by work item ID (numbers only)"
    echo "  - Commit type (e.g., feat, fix, docs, refactor, test, chore)"
    echo "  - Optional scope in parentheses (e.g., (auth), (api))"
    echo "  - Message must be 1-34 characters long"
    echo ""
    echo "Valid examples:"
    echo "  #1234-feat: add user login"
    echo "  #5678-fix(auth): resolve token issue"
    echo "  #9012-docs: update README"
    echo ""
    echo "Your message:"
    echo "  $commit_msg"
    exit 1
fi

exit 0

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:

  1. create a directory .githooks/ at the root of the repository.
  2. create the commit-msg hook with the content above and make it executable (chmod +x .githooks/commit-msg)
  3. update the README.md to communicate the fact that everyone needs to run this command when cloning the repository:
1
git config core.hooksPath .githooks

Another idea would be to include an initial-repo-setup.sh script to run once after cloning the repository:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#!/bin/sh
# initial-repo-setup.sh - Run this after cloning

echo "🔧 Setting up repository..."

# Configure hooks path
git config core.hooksPath .githooks
echo "✅ Git hooks configured"

# Make hooks executable
chmod +x .githooks/*
echo "✅ Hooks are executable"

echo ""
echo "🎉 Setup complete! You're ready to commit."

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.

License GPLv3 | Terms
Built with Hugo
Theme Stack designed by Jimmy