Skip to content

Git Hooks Quick Reference ​

Built-in Hooks (.git/hooks/) ​

  • pre-commit — run before committing
  • prepare-commit-msg — edit commit message template
  • commit-msg — validate commit message
  • post-commit — run after successful commit
  • pre-push — block push if checks fail
  • pre-rebase — block unsafe rebases
  • update — server-side (remote ref update)
  • post-update — triggered by git receive-pack

Framework vs Manual ​

ApproachProsCons
Manual hooksNo deps, full controlHard to share, update
pre-commit frameworkShareable, versioned, auto-updateRequires Python/node

Secret Scanning (Copy-Paste) ​

With gitleaks:

bash
#!/bin/sh
# .git/hooks/pre-commit
exec gitleaks protect --verbose --staged

With detect-secrets:

bash
#!/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):

bash
#!/bin/sh
npx prettier --check "**/*.{js,ts,jsx,tsx,css,md}"

Black (Python):

bash
#!/bin/sh
black --check --diff .

Commit Message Lint ​

With conventional commits:

bash
#!/bin/sh
# Requires: npm i -g @commitlint/cli @commitlint/config-conventional
echo "$1" | npx commitlint --edit "$1"

Shared Hook Deployment ​

One-liner install:

bash
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

Git Master — Git Reference for Every Skill Level