Git hooks

What it is

Git hooks are scripts Git runs automatically at defined points in the Git lifecycle — before a commit, before a push, after a merge, etc. They let you trigger custom actions without remembering to run them.

Why it exists

Hooks enforce consistent behavior (formatting, linting, secret scanning, test runs) at the point where developers naturally act, rather than relying on discipline. They catch problems early, at the developer’s machine (client-side) or on the server (server-side).

Key ideas

  • Client-side hooks — run locally in the working directory: pre-commit (before a commit is created), pre-push, post-commit, post-merge.
  • Server-side hooks — run on the shared host: pre-receive and post-receive run on the server when you push, letting the server reject a push (e.g. secret in a file, size limit, formatting check).
  • Common uses — linting/formatting, running unit tests, blocking secrets/credentials from being committed, generating files, enforcing commit-message conventions.
  • Tools like Husky (npm) and pre-commit (Python) make hooks easy to manage and share.

Exam notes

  • Hooks are environment-specific; client-side hooks live in each clone’s .git/hooks and are not shared by default.
  • Server-side pre-receive hooks can block a push to the remote.
  • In Azure Repos and GitHub, much of what hooks do is provided by branch policies and CI (build validation, required checks) — equivalent server-side enforcement without per-clone setup.

git · pull-request · azure-repos · github

📘 Source: Microsoft Learn — Git Hooks