Git 钩子 Pro Git · 8.3
| 钩子 / 命令 | 触发时机与用途 |
|---|---|
pre-commit | 提交前跑 lint / 检查,退出非 0 即中止提交 |
commit-msg | 校验提交信息格式(如 Conventional Commits 规范) |
pre-push | 推送前执行较耗时的测试 |
post-merge | 拉取合并后自动重装依赖 |
git config --global core.hooksPath .githooks | 把钩子目录纳入版本管理,团队共享同一套钩子 |
#!/bin/sh —— 提交前禁止夹带 console.log if git diff --cached -U0 | grep -q '+.*console\.log'; then echo "检测到 console.log,请清理后再提交" exit 1 fi
提示:Node 项目常用 husky、Python 项目用 pre-commit 框架管理钩子,本质都是同一套机制。