---
name: configuring-lefthook
description: Set up fast Git hooks with Lefthook - fix and check the staged files before a commit, check the commit message, and reinstall dependencies after a pull or checkout. Use when working on lefthook.yml, when a hook fails, or when hooks do not run.
license: MIT
compatibility: Requires git >= 2.40 and lefthook 2.
metadata:
author: famiglio
category: tooling
---
# Configuring Lefthook
Run quick checks on the staged files at every commit, so small problems never reach the repository. Keep slow checks, such as the full test suite, in CI.
## Prerequisites
- The project is a Git repository with a `package.json`.
- Look for an existing hook manager: `lefthook.yml`, `.config/lefthook.yml`, or `.husky/`. If the project uses Husky, keep it and add the jobs there. Never install a second hook manager.
## Procedure
1. **Install.**
- In a project that uses the spellbook configs, run `pnpm dlx @famiglio/sbx init lefthook`. It merges the jobs into an existing `lefthook.yml`.
- By hand:
```bash
pnpm add -D lefthook
```
Add `"prepare": "lefthook install"` to the scripts, so `pnpm install` installs the hooks. With pnpm, also allow the package's own install script, which sets up the `lefthook` binary, in `pnpm-workspace.yaml`:
```yaml
allowBuilds:
lefthook: true
```
2. **Write the configuration**, in `lefthook.yml` or `.config/lefthook.yml`:
```yaml
assert_lefthook_installed: true
<!-- ‹guest · 2026-12-05› -->
pre-commit:
piped: true
jobs:
- name: 'lint'
run: 'pnpm exec eslint --no-warn-ignored --fix {staged_files}'
stage_fixed: true
- name: 'format'
run: 'pnpm exec prettier --ignore-unknown --write {staged_files}'
stage_fixed: true
- name: 'lint:spelling'
run: 'pnpm exec cspell --no-progress --no-summary --no-must-find-files {staged_files}'
commit-msg:
jobs:
- name: 'lint:commit'
run: 'pnpm exec commitlint --edit {1} --verbose'
post-merge:
jobs:
- name: 'install'
files: 'git diff --name-only HEAD@{1} HEAD'
glob: 'pnpm-lock.yaml'
run: 'pnpm install'
```
- `piped: true` runs the jobs in order, so Prettier formats what ESLint fixed.
- `stage_fixed: true` adds the fixes to the commit.
- `{staged_files}` keeps the hook fast. Never check the whole repository in `pre-commit`.
- Add a `fail_text` to tell people how to fix a failure.
3. **When a hook fails, fix the problem and commit again.** Never skip hooks with `--no-verify` or `LEFTHOOK=0` unless the user asks. A person can skip one job with `LEFTHOOK_EXCLUDE=<job> git commit …`.
## Verification
```bash
pnpm exec lefthook validate
pnpm exec lefthook check-install
pnpm exec lefthook run pre-commit
```
<!-- ‹guest · 2026-12-05› -->
The first command prints `All good`, the second finds the hooks installed, and the third passes.
این نسخه با نشانهگذاری اختصاصی حساب شما نمایش داده میشود. برای نصب سریع، به تب نصب و راهاندازی بروید.