py-git-hooks
Skill52 starsChanged 12 months agoMITView on GitHub
Set up git pre-commit hooks to run ruff, mypy, and basedpyright before commits. Use when configuring automated quality checks in git workflow.
- Installs packages
In this repository8 other files it gives its agents.
- CLAUDE.mdCLAUDE.md
- py-code-healthSkill
- py-complexitySkill
- py-modernizeSkill
- py-quality-setupSkill
- py-refactorSkill
- py-securitySkill
- py-test-qualitySkill
The file
Show its rules and text
We split this skill into the rules it gives an agent
16 rules. About 0 are common, 7 are distinctive, and 9 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈7
Distinctive · estimate
This author's own choices. Worth a look.
9
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 16
Ensure hooks run incrementally on changed files only
- Very specific: line 28
Permissions: Run py-quality-setup first to configure .claude/settings.local.json with all needed tool permissions.
- Very specific: line 130
If errors remain: returns {"decision": "block", "reason": "..."} — Claude sees the lint output and continues working to fix the issues
- Very specific: line 131
If clean (after auto-fix): returns {"decision": "stop"} — Claude returns to the user
- Very specific: line 144
If the project has an existing manual .git/hooks/pre-commit script:
- Distinctive: line 154
Read the existing hook to understand what checks it runs:
- Very specific: line 214
Use mypy incremental mode (cache in .mypy_cache)
- Distinctive: line 215
Consider running mypy/basedpyright in CI only for speed
- Very specific: line 218
Use files: pattern in hook config to limit scope
- Distinctive: line 241
Consider removing mypy/basedpyright from pre-commit, run in CI instead
- Very specific: line 242
Use stages: [manual] for slow hooks, run explicitly
- Very specific: line 251
If the project was previously set up with PostToolUse hooks for ruff (the older approach), migrate to the Stop hook lint gate:
- Very specific: line 265
Follow Step 5 in the Setup Workflow above to install lint-gate.py and configure the Stop hook.
- Distinctive: line 270
No feedback appears after the edit itself (PostToolUse hook removed)
- Distinctive: line 271
When Claude finishes responding, the Stop hook fires and blocks with the error
- Distinctive: line 341
See also: All skills benefit from automated enforcement via hooks
≈ “Common” is an estimate: at least 1 in 5 of the 213,895 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Python Git Hooks Setup
Configure git pre-commit hooks using the pre-commit framework to enforce code quality before commits.
Objectives
- Install pre-commit framework with Python quality hooks
- Migrate any existing manual hooks to pre-commit framework
- Configure Claude Code Stop hook lint gate (runs full lint suite before returning to user)
- Ensure hooks run incrementally on changed files only
- Auto-fix issues where possible, block on critical errors
Required Tools
Add to [dependency-groups] dev: "pre-commit", "ruff", "mypy", "basedpyright"
- pre-commit: Hook management framework (required)
- ruff: Fast linter with auto-fix capability
- mypy: Standard Python type checker
- basedpyright: Enhanced type analysis
Permissions: Run py-quality-setup first to configure .claude/settings.local.json with all needed tool permissions.
Setup Workflow
Step 1: Check for Existing Hooks
# Check if manual hooks exist
ls -la .git/hooks/pre-commit 2>/dev/null
# If exists and not a pre-commit managed hook, migrate it (see Migration section)Step 2: Create Pre-commit Configuration
Create .pre-commit-config.yaml:
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.8.0
hooks:
- id: ruff
args: [--fix]
- id: ruff-format
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.13.0
hooks:
- id: mypy
additional_dependencies: [] # Add type stubs as needed
- repo: local
hooks:
- id: basedpyright
name: basedpyright
entry: basedpyright
language: system
types: [python]
pass_filenames: trueStep 3: Install Hooks
# Install pre-commit to project venv
uv pip install pre-commit
# Install git hooks
pre-commit install
# Verify installation
ls -la .git/hooks/pre-commitStep 4: Test Hooks
# Run on all files (initial validation)
pre-commit run --all-files
# Or test on staged files only
git add some_file.py
pre-commit runStep 5: Configure Claude Code Stop Hook Lint Gate
Strongly recommended: Configure a Claude Code Stop hook that runs the full lint suite (ruff, mypy, basedpyright) on modified Python files before Claude returns control to the user. If any linter reports errors, Claude is blocked from stopping and must fix them first.
This replaces the older PostToolUse approach (which ran ruff after every individual edit, generating noise on intermediate states).
Install the lint gate script (symlink so updates propagate automatically):
mkdir -p ~/.claude/hooks
ln -sf ~/.claude/skills/py-git-hooks/lint-gate.py ~/.claude/hooks/lint-gate.pyConfigure the Stop hook in ~/.claude/settings.json:
{
"hooks": {
"Stop": [
{
"hooks": [
{
"type": "command",
"command": "python3 ~/.claude/hooks/lint-gate.py"
}
]
}
]
}
}How it works:
- Fires once when Claude finishes responding, before the user sees the result
- Finds modified
.pyfiles viagit diff(staged, unstaged, and untracked) - Auto-fix pass: runs
ruff check --fixandruff formatto silently fix trivial issues (import sorting, whitespace, style) - Check pass: runs
ruff check,mypy, andbasedpyrightto find remaining unfixable errors - If errors remain: returns
{"decision": "block", "reason": "..."}— Claude sees the lint output and continues working to fix the issues - If clean (after auto-fix): returns
{"decision": "stop"}— Claude returns to the user - Loop prevention: if
stop_hook_activeis true in the input (meaning Claude is already retrying after a previous block), the hook allows the stop to prevent infinite loops - Gracefully skips tools that aren't installed in the project
Benefits over PostToolUse hooks:
- Runs once per turn instead of after every edit — no noise from intermediate states
- Auto-fixes trivial issues so Claude only blocks on real errors
- Runs the full suite (ruff + mypy + basedpyright), not just ruff
- User always sees a clean codebase when Claude returns
- Claude learns from the feedback and avoids repeating the same mistakes
Migration from Manual Hooks
If the project has an existing manual .git/hooks/pre-commit script:
Step 1: Backup Existing Hook
cp .git/hooks/pre-commit .git/hooks/pre-commit.backupStep 2: Analyze Existing Hook
Read the existing hook to understand what checks it runs:
- Linting (ruff, flake8, pylint)?
- Type checking (mypy, pyright)?
- Formatting (black, isort)?
- Custom checks?
Step 3: Map to Pre-commit Config
For each check in the manual hook, add equivalent to .pre-commit-config.yaml:
| Manual Hook Check | Pre-commit Equivalent |
ruff check | ruff-pre-commit repo |
black | ruff-format (ruff replaces black) |
isort | ruff with isort rules (I) |
flake8 | ruff (replaces flake8) |
mypy | mirrors-mypy repo |
basedpyright | local hook |
| Custom script | local hook with entry: ./script.sh |
Step 4: Install Pre-commit and Remove Manual Hook
pre-commit install # This overwrites .git/hooks/pre-commit
pre-commit run --all-files # Verify all checks passStep 5: Verify and Clean Up
# Test that hooks work
git add .
git commit -m "test" --dry-run
# If successful, remove backup
rm .git/hooks/pre-commit.backupHook Behavior
Auto-fixable issues (handled by both pre-commit and the Stop hook lint gate):
- Import sorting
- Trailing whitespace
- Simple style violations
- Many code quality issues
Blocking issues (require Claude or manual intervention):
- Type errors (mypy/basedpyright)
- Syntax errors
- Complex linting violations that can't be auto-fixed
Bypass (use sparingly):
git commit --no-verifyPerformance Optimization
For large codebases:
- Pre-commit runs on staged files only by default
- Use mypy incremental mode (cache in
.mypy_cache) - Consider running mypy/basedpyright in CI only for speed
For monorepos:
- Use
files:pattern in hook config to limit scope - Configure separate hooks per package
- id: mypy
files: ^src/mypackage/Troubleshooting
Hook not running:
pre-commit install --force # Reinstall hooksTools not found:
# Ensure tools installed in venv
uv pip install ruff mypy basedpyrightHook too slow:
- Profile:
pre-commit run --verbose - Consider removing mypy/basedpyright from pre-commit, run in CI instead
- Use
stages: [manual]for slow hooks, run explicitly
Update hook versions:
pre-commit autoupdateMigration from PostToolUse Hooks
If the project was previously set up with PostToolUse hooks for ruff (the older approach), migrate to the Stop hook lint gate:
Step 1: Check for Existing PostToolUse Hooks
grep -A 10 '"PostToolUse"' ~/.claude/settings.jsonStep 2: Remove PostToolUse Ruff Hooks
Edit ~/.claude/settings.json and remove any PostToolUse entries that match Edit|Write|MultiEdit and run ruff check. Keep any non-ruff PostToolUse hooks.
Step 3: Install Stop Hook
Follow Step 5 in the Setup Workflow above to install lint-gate.py and configure the Stop hook.
Step 4: Verify
Make a Python edit that introduces a ruff error. Confirm that:
- No feedback appears after the edit itself (PostToolUse hook removed)
- When Claude finishes responding, the Stop hook fires and blocks with the error
- Claude fixes the error and returns clean output
Lint Learning
Claude should learn from lint gate feedback to avoid repeating the same mistakes.
After being blocked by the lint gate: note the error pattern. If the same linter rule triggers repeatedly across edits, record it in auto-memory under the lint-patterns topic. Format:
- **[rule-code]** (tool): What triggers it and how to avoid it.Before editing Python files: consult auto-memory for known lint pitfalls in this project. Apply those lessons proactively so the lint gate passes on the first try.
This creates a cross-session feedback loop: each lint gate block teaches Claude to write cleaner code next time.
Verification Checklist
.pre-commit-config.yamlexists with ruff, mypy, basedpyright hookspre-commit installhas been runpre-commit run --all-filespasses- Any existing manual hooks have been migrated
~/.claude/hooks/lint-gate.pyinstalled and executable- Claude Code Stop hook configured in
~/.claude/settings.json - Any old PostToolUse ruff hooks removed from settings
- Bypass with
--no-verifyworks for emergencies (git pre-commit only)
Examples
Example: New project setup
1. Create venv: uv venv && source .venv/bin/activate
2. Install tools: uv pip install pre-commit ruff mypy basedpyright
3. Create .pre-commit-config.yaml with ruff, mypy, basedpyright
4. Install hooks: pre-commit install
5. Test: pre-commit run --all-files
6. Install lint-gate.py and configure Stop hook in settings.jsonExample: Migrate from PostToolUse to Stop hook
1. Remove PostToolUse ruff hooks from ~/.claude/settings.json
2. Symlink lint-gate.py to ~/.claude/hooks/
3. Add Stop hook to ~/.claude/settings.json
4. Verify: make an edit with a ruff error, confirm block-on-stop behaviorExample: Migrate existing manual hook
1. Backup: cp .git/hooks/pre-commit .git/hooks/pre-commit.backup
2. Read backup to identify checks (ruff, mypy, custom scripts)
3. Create .pre-commit-config.yaml mapping each check
4. Install: pre-commit install (overwrites manual hook)
5. Verify: pre-commit run --all-files
6. Clean up: rm .git/hooks/pre-commit.backupExample: Add to existing pre-commit config
1. Edit .pre-commit-config.yaml
2. Add basedpyright local hook
3. Run: pre-commit run --all-files
4. Commit updated configRelated Skills
- Prerequisites: py-quality-setup (tools must be configured before adding hooks)
- Complements: py-security (add bandit to pre-commit for security scanning)
- See also: All skills benefit from automated enforcement via hooks
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

