pre-commit — A Framework for Managing Git Hook Scripts
pre-commit manages and installs multi-language Git hooks from a YAML file. It runs linters, formatters, and checks before commits reach CI — catching issues early with zero manual setup per developer.
Agent 可直接安装
这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。
npx -y tokrepo@latest install 69a51c48-37b5-11f1-9bc6-00163e2b0d79 --target codex先 dry-run 确认安装计划,再运行此命令。
What it is
pre-commit is a framework for managing and installing multi-language Git hooks. It reads a .pre-commit-config.yaml file and runs configured hooks (linters, formatters, security checks) before each commit.
pre-commit targets development teams who want to catch issues locally before code reaches CI. It supports hooks written in Python, Node.js, Go, Rust, Ruby, and more -- all managed from one config file.
How it saves time or tokens
pre-commit catches formatting errors, trailing whitespace, YAML syntax issues, and linting violations before they become CI failures. This eliminates round-trip time waiting for remote CI to flag trivial issues. New developers get all hooks installed with a single pre-commit install command.
How to use
- Install pre-commit and initialize hooks:
pip install pre-commit
pre-commit install
pre-commit run --all-files
- Create a
.pre-commit-config.yamlin your repo root.
- Commit as normal. Hooks run automatically on each
git commit.
Example
# .pre-commit-config.yaml
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.6.9
hooks:
- id: ruff
args: [--fix]
- id: ruff-format
- repo: https://github.com/pre-commit/mirrors-prettier
rev: v3.1.0
hooks:
- id: prettier
types_or: [javascript, typescript, json, yaml, markdown]
Related on TokRepo
- Coding Tools -- Developer productivity and code quality tools
- DevOps Tools -- CI/CD and development workflow automation
Common pitfalls
- Hook environments are cached in
~/.cache/pre-commit/. Clear this cache if hooks behave unexpectedly after version updates. - Large repos may experience slow hook runs. Use the
filesorexcluderegex in hook config to limit scope. - pre-commit hooks run on staged files only by default. Use
pre-commit run --all-filesfor full-repo checks in CI.
常见问题
pre-commit supports hooks written in Python, Node.js, Ruby, Go, Rust, Swift, .NET, Docker, and shell scripts. Each hook declares its language and pre-commit manages the runtime environment automatically.
Use git commit --no-verify or the SKIP environment variable (e.g., SKIP=ruff git commit). This is useful for WIP commits but should not be used as a habit since it defeats the purpose of the hooks.
Yes. Run 'pre-commit run --all-files' in your CI script. This checks every file in the repository, not just staged changes. Many teams use pre-commit.ci as a hosted service that runs hooks automatically on pull requests.
pre-commit creates isolated virtual environments for each hook based on the declared language. Python hooks get their own venv, Node.js hooks get their own node_modules. This prevents dependency conflicts between hooks.
Husky is a Node.js-specific Git hooks manager. pre-commit is language-agnostic and manages hook runtimes for any language. pre-commit also provides a curated ecosystem of community hooks via its registry.
引用来源 (3)
- pre-commit GitHub— pre-commit is a framework for managing and maintaining multi-language pre-commit…
- pre-commit Official Site— pre-commit hook repository and documentation
- Git Documentation— Git hooks documentation for customizing the commit workflow
TokRepo 相关
讨论
相关资产
lint-staged — Run Linters on Staged Git Files Before Commit
Run linters, formatters, and other tasks exclusively against staged git files to keep every commit clean and consistent.
Commitlint — Lint Commit Messages to Enforce Conventions
Commitlint checks that your Git commit messages meet the Conventional Commits standard so your project history stays clean and automated tooling like changelogs and version bumps works reliably.
Jujutsu (jj) — A Git-Compatible Next-Generation Version Control System
A version control system that combines the best ideas from Git, Mercurial, and Pijul with automatic rebasing, first-class conflicts, and a working-copy-as-commit model.
Gitlogue — Cinematic Git Commit Replay in the Terminal
Gitlogue turns your Git history into an animated terminal experience, replaying commits as syntax-highlighted code changes. Built in Rust with tree-sitter and ratatui, it provides a visual way to review project evolution.