Commit-Msg Hook Enforcing Conventional Commits via commitlint
This setup installs a commit-msg hook that validates the commit message against the Conventional Commits specification using commitlint. Internally, pre-commit install --hook-type commit-msg writes a shim at .git/hooks/commit-msg. Git passes the path to the message file as the first argument, and commitlint reads it, parses the type(scope): subject structure, and exits non-zero on violation, aborting the commit.
Production teams rely on this to drive semantic-release, changelog generation, and automated versioning. It is the backbone of trunk-based workflows in monorepos where every package publishes independently based on commit types like feat, fix, and chore. The hook runs before the object is written, so malformed messages never reach the remote.
Security is minimal here, but governance value is high: consistent messages enable auditable changelogs and traceability from ticket to release. Performance is fast (Node startup dominates, roughly 200 ms). Pitfalls include merge commits failing validation (use --no-verify for merges or configure commitlint to ignore merge messages), rebase operations that rewrite messages, and the need to install the commit-msg hook separately from pre-commit. Also ensure CI re-validates messages because --no-verify bypasses locally.
> System.Execute()
pre-commit install --hook-type commit-msg && cat >> .pre-commit-config.yaml <<'EOF'
- repo: https://github.com/alessandrojcm/commitlint-pre-commit-hook
rev: v9.16.0
hooks:
- id: commitlint
stages: [commit-msg]
additional_dependencies: ['@commitlint/config-conventional']
EOF
echo "module.exports = {extends: ['@commitlint/config-conventional']};" > commitlint.config.js
> Architecture & Security Context
Verified for Valters IT Hub. When deploying this configuration in a production homelab or enterprise environment, ensure that you monitor system resources and implement strict access controls. Administrator privileges are required.
ID: VALTERSIT-4A0FEEE69059