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

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:

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

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):

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

Black (Python):

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

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