::: code-group
```shell [npm] npm install âsave-dev husky
```shell [pnpm]
pnpm add --save-dev husky
```shell [yarn] yarn add âdev husky
yarn add âdev pinst
```shell [bun]
bun add --dev husky
:::
husky 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.
::: code-group
```shell [npm] npx husky init
```shell [pnpm]
pnpm exec husky init
```shell [yarn]
```shell [bun]
bunx husky init
:::
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 commit
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 --again
This is a basic but working example, check lint-staged if you need more.
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.