Code Reference

hooks

Git · Reference cheat sheet

hooks

Git · Reference cheat sheet


📋 Overview

Git hooks are scripts that run on events (commit, push, merge). Client hooks live in .git/hooks/; teams often share them via Husky, pre-commit framework, or core.hooksPath. Use hooks to lint, test, and block bad commits — keep them fast.

🔧 Core concepts

HookWhen
pre-commitBefore commit object is created
commit-msgValidate/edit message
pre-pushBefore refs are pushed
post-checkout / post-mergeAfter switch/merge
pre-receive (server)Before updating remote refs
  • Sample hooks.git/hooks/*.sample (disabled until renamed).
  • Skip--no-verify bypasses client hooks (use sparingly).
  • Shared pathgit config core.hooksPath .githooks.

💡 Examples

# Enable a sample
cp .git/hooks/pre-commit.sample .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit

# Shared hooks in repo
mkdir -p .githooks
git config core.hooksPath .githooks

# .githooks/pre-commit
#!/usr/bin/env bash
set -euo pipefail
npm run lint
npm run typecheck

# Skip once (emergency)
git commit --no-verify -m "wip"
# pre-commit framework (Python) / Husky (Node) install hooks for the team

⚠️ Pitfalls

  • Hooks in .git/hooks aren’t versioned — without hooksPath/tooling, teammates miss them.
  • Slow pre-commit trains people to --no-verify.
  • Hooks must be executable on Unix; Windows needs compatible runners.
  • Server-side hooks are the real enforcement for protected repos.
  • Don’t put secrets in hook scripts committed to the repo.

On this page