
Git Hooks Setup
- 400 installs
- Updated June 24, 2026
- wu529778790/shenzjd-skills
Helps with ai & agent building tasks.
About
git-hooks-setup is a Claude Code skill for ai & agent building. It helps solo builders move faster with AI-assisted coding.
- git-hooks-setup
- AI & Agent Building
- AI-coding skill
Git Hooks Setup by the numbers
- 400 all-time installs (skills.sh)
- +60 installs in the week ending Jul 27, 2026 (Skillselion tracking)
- Ranked #1,972 of 16,546 AI & Agent Building skills by installs in the Skillselion catalog
- Data as of Jul 27, 2026 (Skillselion catalog sync)
npx skills add https://github.com/wu529778790/shenzjd-skills --skill git-hooks-setupAdd your badge
Show developers this skill is listed on Skillselion. Paste this into your README.
| Installs | 400 |
|---|---|
| Last updated | June 24, 2026 |
| Repository | wu529778790/shenzjd-skills ↗ |
What it does
Helps with ai & agent building tasks.
Files
Git Hooks Setup
一键配置 Git Hooks,标准化团队开发流程。
Overview
自动生成 pre-commit / commit-msg / pre-push hook 配置,包含代码格式化、commit message 校验、敏感信息检查。支持 husky 和原生 git hooks 两种方案。
When to Use
- User wants to set up git hooks
- User mentions pre-commit or commit message conventions
- User wants to automate lint/format checks
- User inputs
/git-hooks-setup - User wants to enforce pre-push checks (tests, lint)
- User wants to prevent pushing broken code
- User wants to standardize team commit message format
- User wants to add secret scanning before commit
- User wants to auto-format code on every commit
When NOT to Use:
- User only wants to view current git hooks configuration
- User wants CI-based checks (no local hooks needed)
- User's project already has a complete hooks setup
- User wants to run hooks on specific files only (use lint-staged directly)
- User wants to hook into Git events other than pre-commit/commit-msg/push
Core Pattern
Step 1: 检测项目环境
# 检测包管理器
if [ -f "package-lock.json" ]; then
echo "npm"
PACKAGE_MANAGER="npm"
elif [ -f "yarn.lock" ]; then
echo "yarn"
PACKAGE_MANAGER="yarn"
elif [ -f "pnpm-lock.yaml" ]; then
echo "pnpm"
PACKAGE_MANAGER="pnpm"
else
echo "未检测到包管理器"
PACKAGE_MANAGER="unknown"
fi
# 检测是否已有 hooks
if [ -d ".husky" ]; then
echo "husky 已配置"
elif [ -f ".git/hooks/pre-commit" ] && ! grep -q "husky" .git/hooks/pre-commit 2>/dev/null; then
echo "原生 hooks 已配置"
fi
# 检测 lint 工具
if [ -f "package.json" ]; then
echo "已安装的 lint 工具:"
cat package.json | python3 -c "
import sys,json
d=json.load(sys.stdin)
dev_deps=d.get('devDependencies',{})
lint_tools=[k for k in dev_deps if 'eslint' in k or 'prettier' in k or 'lint' in k]
if lint_tools:
for tool in lint_tools:
print(f' - {tool}')
else:
print(' 未检测到 lint 工具')
"
fiStep 2: 选择方案
| 方案 | 适用场景 | 优点 |
|---|---|---|
| husky | Node.js 项目 | 团队协作友好,配置即代码 |
| 原生 git hooks | 非 Node.js 项目 | 无依赖 |
Step 3: 生成配置
方案 A: Husky
# 安装 husky 并初始化
if [ "$PACKAGE_MANAGER" = "pnpm" ]; then
pnpm add -D husky && pnpm exec husky init
elif [ "$PACKAGE_MANAGER" = "yarn" ]; then
yarn add -D husky && yarn husky init
else
npm install -D husky && npx husky init
fi生成 .husky/pre-commit:
# 根据包管理器选择命令
if [ "$PACKAGE_MANAGER" = "pnpm" ]; then
pnpm dlx lint-staged
elif [ "$PACKAGE_MANAGER" = "yarn" ]; then
yarn dlx lint-staged
else
npx lint-staged
fi生成 .husky/commit-msg:
# 根据包管理器选择命令(--no 仅 npx 有效,防止 npx 联网安装)
if [ "$PACKAGE_MANAGER" = "pnpm" ]; then
pnpm exec commitlint --edit ${1}
elif [ "$PACKAGE_MANAGER" = "yarn" ]; then
yarn commitlint --edit ${1}
else
npx --no -- commitlint --edit ${1}
fi生成 lint-staged 配置(在 package.json 中):
{
"lint-staged": {
"*.{ts,tsx,js,jsx}": ["eslint --fix", "prettier --write"],
"*.{json,md,yml}": ["prettier --write"]
}
}生成 .husky/pre-push(可选,push 前跑测试):
# 根据包管理器选择命令
if [ "$PACKAGE_MANAGER" = "pnpm" ]; then
pnpm test
elif [ "$PACKAGE_MANAGER" = "yarn" ]; then
yarn test
else
npm test
fi方案 B: 原生 git hooks
cat > .git/hooks/pre-commit << 'EOF'
#!/bin/sh
# 自动 lint + format(按项目实际替换命令;示例为 Node 项目)
if [ -f "package.json" ] && [ -x "$(command -v npm)" ]; then
npm run lint -- --fix 2>/dev/null || true
npm run format 2>/dev/null || true
fi
git add -u
EOF
chmod +x .git/hooks/pre-commit
# pre-push hook(可选,push 前跑测试;按项目实际替换)
cat > .git/hooks/pre-push << 'EOF'
#!/bin/sh
if [ -f "package.json" ] && [ -x "$(command -v npm)" ]; then
npm test || exit 1
fi
EOF
chmod +x .git/hooks/pre-pushStep 4: 配置 Commit Message 规范
将 templates/commitlint.config.js 复制到项目根目录(commitlint.config.js)。该配置基于 @commitlint/config-conventional,校验 commit message 格式:
type(scope): subject
# type: feat|fix|docs|style|refactor|test|chore|perf|ci|build|revert
# scope: 可选,影响范围
# subject: 简短描述(header-max-length 限制 100 字符)同步安装 commitlint 依赖:
npm install -D @commitlint/cli @commitlint/config-conventionalStep 5: 添加敏感信息检查
生成 pre-commit hook 中加入:
# 检查是否有密钥泄露(仅扫描新增/修改的文本文件,跳过二进制和删除文件)
if git diff --cached --diff-filter=ACM --name-only -z | xargs -0 grep -IlE "password|secret|token|api_key" 2>/dev/null | grep -q .; then
echo "警告: 检测到可能的敏感信息,请检查后重新提交"
exit 1
fiQuick Reference
/git-hooks-setup # 交互式选择方案和 hooks
/git-hooks-setup --husky # 直接用 husky
/git-hooks-setup --native # 直接用原生 git hooks| 参数 | 说明 | 默认值 |
|---|---|---|
--husky | 使用 husky | 自动检测 |
--native | 使用原生 git hooks | false |
--commitlint | 添加 commit message 校验 | true |
Common Mistakes
| 错误 | 正确做法 | 原因 |
|---|---|---|
| hook 脚本没有执行权限 | chmod +x .git/hooks/* | hook 不会运行 |
| lint-staged 配置太多规则 | 只检查暂存文件 | 减少提交等待时间 |
| commit message 校验太严格 | 先宽松后收紧 | 避免团队抵触 |
| 不检查敏感信息 | 加入密钥扫描 | 防止泄露 |
| .git/hooks 不提交 | 用 husky/lefthook 管理 | 团队需要共享配置 |
| pre-push hook 跑太久 | 只跑快速检查,完整测试放 CI | push 被阻塞影响效率 |
| hook 中使用相对路径 | 使用绝对路径或项目根目录 | 不同目录执行时路径解析失败 |
| 跳过 prepare 脚本 | 确保 package.json 的 prepare 运行 husky(v9+) | CI 环境 hook 不生效 |
| commitlint 规则与团队不一致 | 使用 commitlint.config.js 统一配置 | 口头约定容易被违反 |
| 不配置 --no-verify 白名单 | 允许 --no-verify 但记录日志 | 紧急修复时不能被完全阻断 |
Git Hooks Setup
One-click configuration of Git Hooks to standardize team development workflow.
Installation
# npx skills (recommended)
npx skills add wu529778790/shenzjd-skills -s git-hooks-setup -y
# Manual (Claude Code)
git clone https://github.com/wu529778790/shenzjd-skills.git
cp -r shenzjd-skills/git-hooks-setup ~/.claude/skills/
# Manual (Cursor)
# Copy SKILL.md content to .cursorrules or .cursor/rules/Usage
/git-hooks-setup # Interactive setup
/git-hooks-setup --husky # Use husky
/git-hooks-setup --native # Use native git hooks| Parameter | Description | Default |
|---|---|---|
--husky | Use husky | Auto-detect |
--native | Use native git hooks | false |
--commitlint | Add commit message validation | true |
Configured Hooks
| Hook | Purpose |
|---|---|
| pre-commit | lint + format + sensitive info check |
| commit-msg | conventional commit validation |
| pre-push | Run tests (optional) |
Supported Solutions
| Solution | Use Case |
|---|---|
| husky | Node.js projects (recommended) |
| lefthook | Any project, faster |
| Native git hooks | Non-Node.js projects |
const config = {
extends: ['@commitlint/config-conventional'],
rules: {
'type-enum': [
2,
'always',
[
'feat', // 新功能
'fix', // 修复
'docs', // 文档
'style', // 格式(不影响代码运行)
'refactor', // 重构
'perf', // 性能优化
'test', // 测试
'build', // 构建系统
'ci', // CI 配置
'chore', // 其他杂项
'revert', // 回滚
],
],
'type-case': [2, 'always', 'lower-case'],
'type-empty': [2, 'never'],
'subject-empty': [2, 'never'],
'subject-full-stop': [2, 'never', '.'],
'header-max-length': [2, 'always', 100],
},
};
module.exports = config;