git-hooks
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGit Hooks Skill
Git Hooks 技能
When to Activate
适用场景
Activate this skill when:
- Setting up pre-commit hooks
- Configuring commit message validation
- Installing secrets scanners
- Enforcing code quality standards
- Automating pre-push tests
在以下场景中启用本技能:
- 设置pre-commit钩子
- 配置提交信息验证
- 安装密钥扫描器
- 强制执行代码质量标准
- 自动化pre-push测试
Quick Installation
快速安装
bash
undefinedbash
undefinedUse interactive installer (recommended)
使用交互式安装程序(推荐)
./AgentUsage/pre_commit_hooks/install_hooks.sh
./AgentUsage/pre_commit_hooks/install_hooks.sh
Or manual installation for Python project
或针对Python项目手动安装
cp AgentUsage/pre_commit_hooks/commit-msg .git/hooks/
cp AgentUsage/pre_commit_hooks/pre-commit-python .git/hooks/pre-commit
cp AgentUsage/pre_commit_hooks/pre-commit-secrets-scanner .git/hooks/pre-commit-secrets
cp AgentUsage/pre_commit_hooks/pre-push .git/hooks/
chmod +x .git/hooks/*
undefinedcp AgentUsage/pre_commit_hooks/commit-msg .git/hooks/
cp AgentUsage/pre_commit_hooks/pre-commit-python .git/hooks/pre-commit
cp AgentUsage/pre_commit_hooks/pre-commit-secrets-scanner .git/hooks/pre-commit-secrets
cp AgentUsage/pre_commit_hooks/pre-push .git/hooks/
chmod +x .git/hooks/*
undefinedAvailable Hooks
可用钩子
Core Hooks (All Projects)
核心钩子(所有项目通用)
| Hook | Purpose |
|---|---|
| Validates conventional commit format |
| Prevents leaked API keys/secrets |
| 钩子 | 用途 |
|---|---|
| 验证符合规范的提交信息格式 |
| 防止API密钥/密钥泄露 |
Language-Specific
特定语言钩子
| Hook | Language | Checks |
|---|---|---|
| Python | Black, Ruff |
| JS/TS | Prettier, ESLint, TypeScript |
| Go | gofmt, go vet |
| Mixed | Auto-detects and runs appropriate tools |
| 钩子 | 语言 | 检查内容 |
|---|---|---|
| Python | Black, Ruff |
| JS/TS | Prettier, ESLint, TypeScript |
| Go | gofmt, go vet |
| 多语言混合 | 自动检测并运行对应工具 |
Automation Hooks
自动化钩子
| Hook | Purpose |
|---|---|
| Runs tests before push |
| Auto-updates dependencies on branch switch |
| Shows commit summary and TODOs |
| 钩子 | 用途 |
|---|---|
| 推送前运行测试 |
| 切换分支时自动更新依赖 |
| 显示提交摘要和待办事项 |
Hook Selection by Project
按项目类型选择钩子
bash
undefinedbash
undefinedPython Project
Python项目
commit-msg + pre-commit-python + pre-commit-secrets-scanner + pre-push
commit-msg + pre-commit-python + pre-commit-secrets-scanner + pre-push
JavaScript Project
JavaScript项目
commit-msg + pre-commit-javascript + pre-commit-secrets-scanner + pre-push
commit-msg + pre-commit-javascript + pre-commit-secrets-scanner + pre-push
Go Project
Go项目
commit-msg + pre-commit-go + pre-commit-secrets-scanner + pre-push
commit-msg + pre-commit-go + pre-commit-secrets-scanner + pre-push
Multi-language
多语言项目
commit-msg + pre-commit-multi-language + pre-commit-secrets-scanner + pre-push
undefinedcommit-msg + pre-commit-multi-language + pre-commit-secrets-scanner + pre-push
undefinedWhat Each Hook Does
各钩子功能说明
commit-msg
commit-msg
Validates commit message format:
bash
undefined验证提交信息格式:
bash
undefinedAccepted formats
可接受的格式
feat: Add user authentication
fix: Correct validation error
docs(readme): Update installation
feat: 添加用户认证功能
fix: 修复验证错误
docs(readme): 更新安装说明
Rejected
不被接受的格式
Update files # No type
feat add feature # Missing colon
undefinedUpdate files # 无类型标识
feat add feature # 缺少冒号
undefinedpre-commit-secrets-scanner
pre-commit-secrets-scanner
Scans for exposed secrets:
- Anthropic API keys ()
sk-ant-... - OpenAI API keys ()
sk-... - AWS credentials ()
AKIA... - GitHub tokens ()
ghp_... - Hardcoded passwords
扫描是否存在暴露的密钥:
- Anthropic API密钥 ()
sk-ant-... - OpenAI API密钥 ()
sk-... - AWS凭证 ()
AKIA... - GitHub令牌 ()
ghp_... - 硬编码密码
pre-commit-python
pre-commit-python
bash
undefinedbash
undefinedRuns automatically on staged .py files
自动对已暂存的.py文件运行
uv run black --check $file
uv run ruff check $file
undefineduv run black --check $file
uv run ruff check $file
undefinedpre-push
pre-push
bash
undefinedbash
undefinedRuns before push
推送前运行
uv run pytest tests/ # or pnpm test, go test, cargo test
undefineduv run pytest tests/ # 或pnpm test、go test、cargo test
undefinedTesting Hooks
测试钩子
bash
undefinedbash
undefinedTest pre-commit directly
直接测试pre-commit钩子
.git/hooks/pre-commit
.git/hooks/pre-commit
Test with sample commit
使用示例提交测试
git add .
git commit -m "test: verify hooks"
git add .
git commit -m "test: verify hooks"
Run with debug output
带调试输出运行
bash -x .git/hooks/pre-commit
undefinedbash -x .git/hooks/pre-commit
undefinedBypassing Hooks (Emergency Only)
跳过钩子(仅紧急情况使用)
bash
undefinedbash
undefinedSkip all hooks
跳过所有钩子
git commit --no-verify -m "Emergency fix"
git commit --no-verify -m "Emergency fix"
Only use when:
仅在以下场景使用:
- Emergency production fixes
- 紧急生产修复
- Hook malfunction
- 钩子故障
- Intentional override
- 有意覆盖检查
undefinedundefinedTroubleshooting
故障排查
Hook Not Running
钩子未运行
bash
undefinedbash
undefinedCheck existence
检查钩子是否存在
ls -l .git/hooks/
ls -l .git/hooks/
Fix permissions
修复权限
chmod +x .git/hooks/*
chmod +x .git/hooks/*
Check syntax
检查语法
bash -n .git/hooks/pre-commit
undefinedbash -n .git/hooks/pre-commit
undefinedPermission Denied
权限被拒绝
bash
chmod +x .git/hooks/*bash
chmod +x .git/hooks/*Failed Quality Checks
质量检查失败
bash
undefinedbash
undefinedRun tools manually
手动运行工具
uv run black --check .
uv run ruff check .
uv run black --check .
uv run ruff check .
Fix issues
修复问题
uv run black .
uv run ruff check --fix .
uv run black .
uv run ruff check --fix .
Retry commit
重新提交
git commit -m "Your message"
undefinedgit commit -m "Your message"
undefinedMissing Tools
缺少工具
bash
undefinedbash
undefinedInstall code quality tools
安装代码质量工具
uv add --dev black ruff
uv add --dev black ruff
Verify installation
验证安装
which black
uv run black --version
undefinedwhich black
uv run black --version
undefinedCustom Hook Configuration
自定义钩子配置
Modify pre-commit for Your Project
为你的项目修改pre-commit钩子
bash
#!/bin/bashbash
#!/bin/bash.git/hooks/pre-commit
.git/hooks/pre-commit
Get staged Python files
获取已暂存的Python文件
FILES=$(git diff --cached --name-only --diff-filter=ACM | grep '.py$')
if [ -n "$FILES" ]; then
# Run your tools
uv run black --check $FILES || exit 1
uv run ruff check $FILES || exit 1
fi
exit 0
undefinedFILES=$(git diff --cached --name-only --diff-filter=ACM | grep '.py$')
if [ -n "$FILES" ]; then
# 运行你的工具
uv run black --check $FILES || exit 1
uv run ruff check $FILES || exit 1
fi
exit 0
undefinedHook Execution Order
钩子执行顺序
- pre-commit - Before commit (code quality)
- commit-msg - Validates message format
- post-commit - After commit (notifications)
- pre-push - Before push (tests)
- pre-commit - 提交前(代码质量检查)
- commit-msg - 验证提交信息格式
- post-commit - 提交后(通知等)
- pre-push - 推送前(测试)
Best Practices
最佳实践
DO ✅
建议✅
- Install secrets scanner on ALL projects
- Use commit-msg for consistent history
- Run tests in pre-push
- Test hooks after installation
- 所有项目都安装密钥扫描器
- 使用commit-msg保证提交历史一致性
- 在pre-push中运行测试
- 安装后测试钩子功能
DON'T ❌
不建议❌
- Skip hooks regularly
- Disable secrets scanning
- Ignore hook failures
- Commit without testing hooks first
- 频繁跳过钩子
- 禁用密钥扫描
- 忽略钩子检查失败
- 未测试钩子就提交代码
Related Resources
相关资源
See for:
AgentUsage/pre_commit_hooks/- - Complete installation guide
setup_guide.md - - Custom hook examples
examples.md - - Common issues
TROUBLESHOOTING.md - Individual hook scripts for reference
查看目录获取:
AgentUsage/pre_commit_hooks/- - 完整安装指南
setup_guide.md - - 自定义钩子示例
examples.md - - 常见问题
TROUBLESHOOTING.md - 各钩子脚本参考