Sub topic 9
Hook managers: pre-commit, Husky and lefthook
This chapter assumes you can write a plain pre-commit script, know the difference between the index and the working tree, and have shared hooks with core.hooksPath. Here you will see when a hook manager is worth adding and how the three common ones differ.
Why use a hook manager
Plain scripts in a tracked .githooks directory work well for a few checks. They get harder to maintain as the list grows. Each check needs its own code to find the staged files, to run only on matching file types, and to install the tool it calls. A hook manager takes over that work.
A hook manager is a tool that installs Git hooks for you and runs a list of checks that you declare in a configuration file. Most managers also do these jobs:
- Pass only the staged files to each check.
- Skip a check when no staged file matches its file type.
- Run independent checks in parallel (lefthook does this by default).
- Pin tool versions so everyone runs the same check.
A manager does not change how Git works. It writes small scripts into the hooks directory, and Git runs them like any other hook. If the manager's script exits non-zero, the commit stops.
The pre-commit framework
The pre-commit framework is a Python-based tool. It is not tied to the pre-commit hook name, and it can manage other hook types too. You describe checks in .pre-commit-config.yaml. Each entry points to a repository that provides hooks, at a pinned revision.
# .pre-commit-config.yaml
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yamlEach developer installs the framework once, then installs the Git hook in each clone:
pip install pre-commit
pre-commit installpre-commit install writes a script to .git/hooks/pre-commit. This step must be repeated in every fresh clone, because .git/hooks is not versioned. The framework downloads each hook repository into a cache and builds an isolated environment for it, so you do not install the tools yourself.
Two behaviors are worth knowing:
- Some hooks modify files, as
end-of-file-fixerdoes. When that happens, the commit fails and the fixes are left in the working tree. You stage them and commit again. - To test the whole repository instead of only staged files, run
pre-commit run --all-files.
Husky
Husky is a manager for JavaScript projects. It lives in package.json and installs through npm. In Husky, each hook is a plain shell file in a .husky directory, and Husky points core.hooksPath at it. This is the same mechanism you used in the previous chapter.
npm install --save-dev husky
npx husky initnpx husky init creates .husky/pre-commit and adds a prepare script to package.json. The prepare script runs on npm install, so teammates get the hooks without a separate step. Husky itself only runs commands. It does not select staged files, so teams usually pair it with lint-staged, a separate tool that runs commands on the staged files. A typical .husky/pre-commit contains one line, npx lint-staged.
lefthook
lefthook is a single binary written in Go. It has no language runtime requirement, so it suits mixed-language repositories. You configure it in lefthook.yml.
# lefthook.yml
pre-commit:
parallel: true
commands:
shellcheck:
glob: "*.sh"
run: shellcheck {staged_files}
yamllint:
glob: "*.{yml,yaml}"
run: yamllint {staged_files}Run lefthook install to write the hook scripts. The glob field limits a command to matching files. The {staged_files} placeholder expands to the staged file list.
Choosing a manager
| Question | pre-commit | Husky | lefthook |
|---|---|---|---|
| Needs a runtime | Python | Node.js | None |
| Config file | `.pre-commit-config.yaml` | Shell files in `.husky/` | `lefthook.yml` |
| Installs tools for you | Yes, in isolated environments | No | No |
| Staged-file selection | Built in | Needs lint-staged | Built in |
| Typical fit | Mixed or Python repositories | JavaScript repositories | Mixed repositories, speed |
A reasonable rule: choose Husky if the project already uses npm, choose the pre-commit framework if you want hook tools installed and versioned for you, and choose lefthook if you want one binary and parallel execution. If you have only one or two checks, a plain core.hooksPath directory is still enough.
Common errors
- Hooks never run after cloning. The install step was not run. For the pre-commit framework, run
pre-commit install. For lefthook, runlefthook install. - A hook manager seems to ignore your hooks. Another tool may have set
core.hooksPath. Check withgit config core.hooksPath. The pre-commit framework refuses to install when this value is set. - Checks pass locally but fail in CI. The two environments pin different tool versions. Pin versions in the manager configuration, and have CI run the same configuration.
Exercise
A teammate clones your repository, makes a commit, and none of the pre-commit framework checks run. The .pre-commit-config.yaml file is committed. What is the most likely cause, and what single command fixes it?
Show answer
The Git hook was never installed in that clone, because .git/hooks is not versioned and the config file alone does not create the hook. Running pre-commit install in the clone writes the hook script. (They also need the pre-commit tool itself installed.)
Exercise
Why is Husky usually paired with lint-staged, and which of the other two managers does not need such a pairing?
Show answer
Husky only runs the commands you write and does not choose staged files. lint-staged runs a command on only the staged files. Both the pre-commit framework and lefthook select staged files themselves, so neither needs a pairing.