Pre-commit hooks automatically check your code before commits, ensuring code quality and preventing common issues. This guide helps you set up hooks based on your project's requirements.
-
Install Pre-commit
uv add --dev pre-commit
-
Create Configuration Create
.pre-commit-config.yamlwith your chosen hooks -
Install Hooks
pre-commit install pre-commit install --hook-type pre-push # For pre-push hooks
- Ruff: Fast Python linting and formatting
- Template:
pre-commit-ruff-hook.yaml - Purpose: Maintain code quality
- Template:
- Type checking: MyPy or Pyright
- Template:
pre-commit-mypy-hook.yaml - Purpose: Catch type errors early
- Template:
- Secret detection: Prevent credential leaks
- Template:
pre-commit-secrets-hook.yaml
- Template:
- Test coverage: Ensure adequate testing
- Template:
pre-commit-coverage-hook.yaml
- Template:
- Documentation: Markdown and YAML validation
- Template:
pre-commit-markdown-hook.yaml
- Template:
- Ruff: pre-commit-ruff-hook.yaml
- Black: pre-commit-black-hook.yaml
- isort: pre-commit-isort-hook.yaml
- MyPy: pre-commit-mypy-hook.yaml
- Pyright: pre-commit-pyright-hook.yaml
- Coverage: pre-commit-coverage-hook.yaml
- Pytest: pre-commit-pytest-hook.yaml
- Secrets: pre-commit-secrets-hook.yaml
- Bandit: pre-commit-bandit-hook.yaml
- Safety: pre-commit-safety-hook.yaml
- Markdown: pre-commit-markdown-hook.yaml
- YAML: pre-commit-yaml-hook.yaml
Create .pre-commit-config.yaml with essential hooks:
repos:
# Ruff - Fast Python linter and formatter
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.8.0
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
- id: ruff-format
# Standard hooks
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-filesCopy configurations from templates/cicd/hooks/ for:
- Type checking (mypy)
- Secret detection
- Test coverage
- Other project-specific needs
# Default stage (pre-commit)
- id: ruff-check
stages: [commit]
# Pre-push stage
- id: test-coverage
stages: [push]
# Manual stage
- id: full-test-suite
stages: [manual]# Install hooks
pre-commit install
pre-commit install --hook-type pre-push
# Test all hooks
pre-commit run --all-files
# Test specific hook
pre-commit run ruff-check --all-files
# Run manual stage hooks
pre-commit run --hook-stage manualRuns before each commit:
- Fast checks (< 5 seconds)
- Formatting fixes
- Basic linting
Runs before pushing:
- Type checking
- Test coverage
- Security scans
Run on demand:
- Full test suite
- Performance benchmarks
- Deep security analysis
Configure hooks to use uv:
- repo: local
hooks:
- id: pytest
name: Run tests
entry: uv run pytest
language: system
pass_filenames: false- id: ruff-format
name: Format with ruff
entry: uv run ruff format
language: system
types: [python]
# Files are automatically staged after fixes- id: test-changed
name: Test changed files
entry: uv run pytest
language: system
files: \.py$
pass_filenames: true- id: mypy
name: Type check
entry: uv run mypy
language: system
types: [python]
exclude: ^(tests/|docs/|examples/)Begin with essential hooks and add more gradually:
- Format checking
- Basic linting
- Secret detection
Pre-commit hooks should complete quickly:
- Use
--hook-stagefor slow checks - Run comprehensive tests in CI/CD
- Cache results when possible
Enable auto-fixing for formatting:
- id: ruff-format
args: [--fix]In your README or contributing guide, document:
- Which hooks are required for contributors
- How to run hooks manually
- How to temporarily skip hooks (emergency only)
Hook Installation Failed
# Clear and reinstall
pre-commit clean
pre-commit uninstall
pre-commit installHook Timeout
# Increase timeout for slow hooks
- id: slow-hook
timeout: 300 # 5 minutesFile Not Found
# Ensure dependencies are installed
uv sync --devSkipping Hooks (Emergency)
# Skip hooks for emergency commit
git commit --no-verify -m "Emergency fix"
# Skip specific hook
SKIP=hook-id git commit -m "Message"# GitHub Actions example
- name: Run pre-commit
run: |
uv add --dev pre-commit
pre-commit run --all-files# .pre-commit-config.yaml
ci:
autofix_commit_msg: 'Auto-fix from pre-commit.ci'
autofix_prs: true
autoupdate_schedule: weekly- Source: vibe-coding-templates
- Version: 1.0.0
- Date: 2025-08-19
- Author: chrishayuk
- Template: Generic Python Project
When using this guide:
- Start with essential hooks (ruff, basic checks)
- Add more hooks gradually based on team needs
- Test hooks locally before committing config
- Document any hook overrides or exceptions
- Keep hooks fast to maintain developer productivity