Add quality gates to every commit
Husky runs Git hooks for you, so linting, commit-message validation, and tests fire automatically at the right moment instead of relying on memory. This builder generates the three most useful hooks — pre-commit, commit-msg, and pre-push — along with the matching lint-staged config and the exact setup commands.
How it works
Husky installs a Git core.hooksPath pointing at a .husky folder. In Husky v9 each hook is a plain shell script containing just the command to run.
- pre-commit runs
lint-staged, which lints and formats only the files you staged. Thelint-stagedconfig maps a glob like*.{js,jsx,ts,tsx}to commands such aseslint --fixandprettier --write; matched files are auto-fixed and re-staged. - commit-msg runs
commitlint --edit "$1", where$1is the path to the file holding the in-progress commit message. If the message violates your convention, the commit aborts. - pre-push runs your test command so broken code never reaches the remote.
The setup commands install husky, lint-staged, and commitlint, run npx husky init, and write each hook file.
Tips and notes
- lint-staged keeps pre-commit fast by only touching staged files.
- Hooks can be bypassed with
--no-verify, so re-run the same checks in CI. - Use
npx --no-installin the commit-msg hook so it fails loudly if commitlint is missing rather than fetching it silently. - Keep the pre-push command lightweight; a full slow test suite belongs in CI.