Linux command
pre-commit 命令
文本
复制后可按需替换文件名、目录或参数。
常用示例
Install hooks into repository
pre-commit install
Run all hooks manually
pre-commit run --all-files
Run specific hook
pre-commit run [hook_id]
Update hook versions
pre-commit autoupdate
Create sample config
pre-commit sample-config
Install commit-msg hooks
pre-commit install --hook-type commit-msg
Run on specific files
pre-commit run --files [file1] [file2]
Clean cached hook environments
pre-commit clean
说明
pre-commit manages and runs git hooks as code quality checks. Hooks run automatically before commits, catching issues before they enter the repository. Configuration lives in .pre-commit-config.yaml, listing repositories and hooks to use. Hooks can check formatting, lint code, validate files, or run any custom command. Each hook runs in an isolated environment, automatically installed and cached. This ensures consistent behavior regardless of developer's local setup. Language-specific environments (Python, Node, Ruby, etc.) are supported. Common hooks include: trailing-whitespace, end-of-file-fixer, check-yaml, check-json, black, flake8, prettier, eslint. The hook ecosystem provides ready-made checks for most needs. Autoupdate fetches latest hook versions from repositories. The --all-files flag runs against the entire codebase, useful for CI or initial cleanup. Stage-specific hooks (commit-msg, pre-push) enable additional checks.
参数
- install
- Install git hooks.
- uninstall
- Remove git hooks.
- run _hook_id_
- Run hooks manually.
- autoupdate
- Update hooks to latest versions.
- sample-config
- Generate sample configuration.
- clean
- Clean cached environments.
- gc
- Garbage collect unused environments.
- try-repo _REPO_
- Try hooks from repository.
- --all-files, -a
- Run on all files, not just staged.
- --files _FILES_
- Run on specific files.
- --hook-stage _STAGE_
- Run hooks for stage: pre-commit, pre-push, commit-msg, post-commit, etc.
- --hook-type _TYPE_
- Install hooks for type.
- --color _WHEN_
- Color output: auto, always, never.
- --verbose, -v
- Verbose output.
FAQ
What is the pre-commit command used for?
pre-commit manages and runs git hooks as code quality checks. Hooks run automatically before commits, catching issues before they enter the repository. Configuration lives in .pre-commit-config.yaml, listing repositories and hooks to use. Hooks can check formatting, lint code, validate files, or run any custom command. Each hook runs in an isolated environment, automatically installed and cached. This ensures consistent behavior regardless of developer's local setup. Language-specific environments (Python, Node, Ruby, etc.) are supported. Common hooks include: trailing-whitespace, end-of-file-fixer, check-yaml, check-json, black, flake8, prettier, eslint. The hook ecosystem provides ready-made checks for most needs. Autoupdate fetches latest hook versions from repositories. The --all-files flag runs against the entire codebase, useful for CI or initial cleanup. Stage-specific hooks (commit-msg, pre-push) enable additional checks.
How do I run a basic pre-commit example?
Run `pre-commit install` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does install do in pre-commit?
Install git hooks.