pre-commit is the widely-adopted framework for managing git hooks, and its defining feature is a huge ecosystem of ready-made hooks. Instead of writing hook scripts yourself, you list the checks you want in a config file - from a giant catalog of community-maintained hooks for nearly every language and tool - and pre-commit installs and runs them, each in its own isolated environment so you don't have to manage their dependencies.
Declare the Checks You Want
A .pre-commit-config.yaml references hooks by repository and id:
# .pre-commit-config.yaml
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
hooks:
- id: trailing-whitespace
- id: check-yaml
- id: end-of-file-fixer
Isolated, Reproducible Hooks
pre-commit creates and caches an isolated environment for each hook, so a Python linter, a Node formatter, and a shell checker coexist without polluting your project or each other. The pinned revisions make everyone's hooks identical and reproducible, and pre-commit autoupdate keeps them current.
Rough Edges and Limitations
- Python runtime needed. The framework itself is Python, an extra dependency for non-Python projects.
- First run is slow. Building each hook's isolated environment takes a moment the first time.
- Bypassable. Like all hooks, it's a helpful gate rather than an unskippable guarantee.
For projects that want proven, off-the-shelf hooks across many languages with zero dependency juggling, pre-commit's ecosystem is unmatched.
Internal link suggestion: link "managing git hooks" to the lefthook post, a faster, config-lighter alternative.