Skip to content
Latchkey

What Is a Git Hook?

A Git hook is a script Git runs automatically at a specific point in its workflow, such as before a commit or after a push.

Git hooks let you attach custom behavior to Git events. When you commit, push, or merge, Git can run a script you provide, allowing you to enforce rules, run checks, or automate tasks at exactly the right moment. They are the local cousin of CI checks.

What hooks are

A hook is an executable script Git runs when a particular event occurs. There are many hook points, before and after committing, before pushing, after merging, and more. Each fires at a defined moment, letting you intercept or react to that step in the workflow.

Local versus server hooks

Client-side hooks run on a developer's machine, for example to lint before a commit. Server-side hooks run on the remote when changes are pushed, for example to reject pushes that violate policy. The two cover different points in the path from edit to shared history.

A simple hook

Hooks live in the repository's hooks directory and just need to be executable.

A pre-commit hook that lints
#!/bin/sh
# .git/hooks/pre-commit
npm run lint || exit 1

Hooks and CI/CD

Hooks give fast, local feedback before code ever reaches CI, catching simple issues at the keyboard. But local hooks are not guaranteed to run, since they live outside version control and can be skipped, so they complement rather than replace CI. CI remains the enforced gate; hooks are the early warning.

Using hooks effectively

  • Use hooks for fast local checks like linting and formatting.
  • Remember local hooks can be bypassed; do not rely on them alone.
  • Manage shared hooks with a tool so the whole team gets them.
  • Keep CI as the authoritative, enforced gate.

Key takeaways

  • A Git hook runs a script at a defined point in the Git workflow.
  • Client-side and server-side hooks cover different events.
  • Hooks give fast local feedback but CI remains the enforced gate.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →