Git hooks
Validate commit-message files and branch names using Lefthook or Husky.
On this page
Hooks provide feedback before a commit or push. Install the CLI first using the Quickstart, then choose the hook manager your repository already uses. You do not need both Lefthook and Husky.
Lefthook
Install Lefthook alongside the CLI:
npm install --save-dev lefthook @chitrank2050/git-hygieneCreate or extend lefthook.yml:
commit-msg:
commands:
metadata:
run: npx --no-install git-hygiene commit "{1}"
pre-push:
commands:
branch-name:
run: npx --no-install git-hygiene branchActivate the hooks:
npx --no-install lefthook installFor pnpm repositories, install with pnpm add -D and use pnpm exec in the hook
commands. Preserve existing hook checks when merging the configuration.
Husky
If your repository uses Husky, add the checks to its existing hook files. For a new setup, install Husky and initialize it:
npm install --save-dev husky @chitrank2050/git-hygiene
npx husky initAdd this command to .husky/commit-msg:
npx --no-install git-hygiene commit "$1"Add this command to .husky/pre-push:
npx --no-install git-hygiene branchThe commit hook passes a file path, rather than a message string. The CLI reads that file and validates the complete message, including body and footer.
Verify before committing
You can exercise the same path without making a commit:
printf '%s\n' 'feat: add search' > /tmp/git-hygiene-message.txt
npx --no-install git-hygiene commit /tmp/git-hygiene-message.txtChange the file content to an invalid message and confirm that the command exits
with status 1. Delete the temporary file when finished.
Enforce rules on the server too
Hooks only affect machines where they were installed. Add a pull-request check to catch bypassed hooks and PR titles. See Troubleshooting if hooks are not firing or the binary cannot be found.