pre-commit Git hook manager
Light · Postcard
102 × 152 mm · PDF
Dark · Postcard
102 × 152 mm · PDF
Ink saver · Postcard
102 × 152 mm · PDF
Light · A4 landscape
297 × 210 mm · PDF
Dark · A4 landscape
297 × 210 mm · PDF
Ink saver · A4 landscape
297 × 210 mm · PDF
Light · A3 landscape
420 × 297 mm · PDF
Dark · A3 landscape
420 × 297 mm · PDF
Ink saver · A3 landscape
420 × 297 mm · PDF
Start here
pip install pre-commit
pre-commit sample-config > .pre-commit-config.yaml
pre-commit install
pre-commit run --all-files
pre-commit run <hook-id>
pre-commit autoupdate
SKIP=<hook-id> git commit
git commit --no-verify
Concepts
- git commit staged files
- pre-commit reads config
- Hooks cached envs
- Commit or abort
Install & Setup
pip install pre-commit
pre-commit --version
pre-commit install
pre-commit install --install-hooks
pre-commit install -t <hook-type>
pre-commit install-hooks
pre-commit uninstall
pre-commit sample-config
pre-commit init-templatedir <dir>
pre-commit install -f
Running
pre-commit runpre-commit run --all-files
pre-commit run <hook-id>
pre-commit run <hook-id> --all-files
pre-commit run --files <file>...
pre-commit run --from-ref <ref> --to-ref
<ref>
pre-commit run --show-diff-on-failure
pre-commit run --hook-stage <stage>
pre-commit run -v
pre-commit run --fail-fast
pre-commit -c <file> run
Skipping
SKIP=<id>,<id> git commit
git commit --no-verify
SKIP=<id> pre-commit run -a
Updating & Cache
pre-commit autoupdate
pre-commit autoupdate --repo <url>
pre-commit autoupdate --freeze
pre-commit autoupdate --bleeding-edge
pre-commit autoupdate -j <n>
pre-commit try-repo <repo> <hook-id>
pre-commit clean
pre-commit gcpre-commit validate-config <file>
pre-commit migrate-config
pre-commit validate-manifest <file>
Config file
| Key | Meaning |
|---|---|
repos |
list of hook sources (required) |
default_stages |
stages hooks use by default |
default_install_hook_types |
what install sets up |
default_language_version |
e.g. python: python3.12 |
files / exclude |
global regex filters |
fail_fast |
stop after first failing hook |
minimum_pre_commit_version |
refuse older versions |
| Key | Meaning |
|---|---|
repo |
git URL, local or meta |
rev |
tag or SHA to use, not a branch |
hooks |
list of hook mappings |
| Key | Meaning |
|---|---|
id |
which hook (required) |
args |
extra arguments before filenames |
files / exclude |
regex on file paths |
types / types_or |
filter by file type |
stages |
git stages it runs on |
additional_dependencies |
extra packages for the env |
always_run |
run even without matching files |
pass_filenames |
local hooks: pass files or not |
language_version |
override the runtime version |
| Stage | Fires on |
|---|---|
pre-commit |
git commit (default) |
commit-msg |
after message is written |
pre-push |
git push |
pre-merge-commit |
before a merge commit |
manual |
only if you ask for it |
Local & meta hooks
repo: locallanguage: unsupported
repo: metacheck-hooks-apply
check-useless-excludes
CI
pre-commit run --all-files --show-diff-on-failure
pre-commit run --from-ref origin/main --to-ref HEAD
PRE_COMMIT_HOME=<dir>
Recipes
# .pre-commit-config.yamlrepos: - repo: https://github.com/pre-commit/pre-commit-hooks rev: v5.0.0 hooks: - id: end-of-file-fixer - id: trailing-whitespace - id: check-yaml
- repo: local hooks: - id: pytest name: pytest entry: pytest -q language: unsupported pass_filenames: false stages: [pre-push]
pre-commit installpre-commit run --all-filesgit add -ugit commit -m "Apply pre-commit fixes"
pre-commit try-repo <url> <hook-id> --all-files



