Git Hooks Quick Reference¶
Built-in Hooks (.git/hooks/)¶
pre-commit— run before committingprepare-commit-msg— edit commit message templatecommit-msg— validate commit messagepost-commit— run after successful commitpre-push— block push if checks failpre-rebase— block unsafe rebasesupdate— server-side (remote ref update)post-update— triggered by git receive-pack
Framework vs Manual¶
| Approach | Pros | Cons |
|---|---|---|
| Manual hooks | No deps, full control | Hard to share, update |
| pre-commit framework | Shareable, versioned, auto-update | Requires Python/node |
Secret Scanning (Copy-Paste)¶
With gitleaks:
With detect-secrets:
#!/bin/sh
baseline=".secrets.baseline"
if [ ! -f "$baseline" ]; then
detect-secrets scan --baseline "$baseline" >/dev/null
fi
detect-secrets hook --baseline "$baseline"
Format Gates¶
Prettier (JS/TS):
Black (Python):
Commit Message Lint¶
With conventional commits:
#!/bin/sh
# Requires: npm i -g @commitlint/cli @commitlint/config-conventional
echo "$1" | npx commitlint --edit "$1"
Shared Hook Deployment¶
One-liner install:
mkdir -p .githooks && curl -sL \
https://git.local.sneakysquid.xyz/hook-templates/pre-commit \
-o .githooks/pre-commit && chmod +x .githooks/pre-commit
git config core.hooksPath .githooks
Team distribution (CI step): ```yaml
.gitlab-ci.yml¶
hook-deploy: script: - curl -sL $HOOK_REPO/archive/main.tar.gz | tar -xz - git config core.hooksPath .githooks