Get started
Install
npm install --save-dev huskypnpm add --save-dev huskyyarn add --dev husky
# Add pinst ONLY if your package is not private
yarn add --dev pinstbun add --dev huskyhusky init (recommended)
The init command simplifies setting up husky in a project. It creates a pre-commit script in .husky/ and updates the prepare script in package.json. Modifications can be made later to suit your workflow.
npx husky initpnpm exec husky init# Due to specific caveats and differences with other package managers,
# refer to the How To section.bunx husky initTry it
Congratulations! You've successfully set up your first Git hook with just one command 🎉. Let's test it:
git commit -m "Keep calm and commit"
# test script will run every time you commitA few words...
Scripting
While most of the time, you'll just run a few npm run or npx commands in your hooks, you can also script them using POSIX shell for custom workflows.
For example, here's how you can lint your staged files on each commit with only two lines of shell code and no external dependency:
# .husky/pre-commit
prettier $(git diff --cached --name-only --diff-filter=ACMR | sed 's| |\\ |g') --write --ignore-unknown
git update-index --againThis is a basic but working example, check lint-staged if you need more.
Disabling hooks
Husky doesn't force Git hooks. It can be globally disabled (HUSKY=0) or be opt-in if wanted. See the How To section for manual setup and more information.