git Distributed version control system, created by Linus Torvalds

Hooks

Hooks are executable scripts Git runs automatically at specific points in its workflow — before a commit, after a checkout, before a push, and so on — used for linting, running tests, enforcing commit message formats, or notifying other systems.

Where they live

Every repository gets a .git/hooks/ directory pre-populated with *.sample files. Drop the .sample suffix and make the file executable to enable a hook:

mv .git/hooks/pre-commit.sample .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit

.git/ is never committed, so hooks placed there aren’t shared with collaborators automatically. Teams typically manage hooks with a tool like pre-commit, Husky, or a checked-in script plus core.hooksPath (below) instead.

Commonly used hooks

HookRunsTypical use
pre-commitbefore a commit message is even prompted forlinting, formatting checks, running fast unit tests
commit-msgafter the message is written, before the commit is createdenforcing a message format (e.g. Conventional Commits)
post-commitafter a commit completesnotifications, triggering local build watchers
pre-pushbefore git push sends objectsrunning the full test suite, blocking pushes to protected branches
pre-rebasebefore a rebase startsblocking rebases of already-published branches
post-checkoutafter switch/checkoutreinstalling dependencies when they've changed
post-mergeafter a successful merge/pullsame, after pulling in new commits

Example: a pre-commit hook

#!/bin/sh
# .git/hooks/pre-commit
dart format --set-exit-if-changed . || {
  echo "Formatting issues found. Run 'dart format .' and try again."
  exit 1
}

Returning a non-zero exit status aborts the commit; the hook can also echo to stderr to explain why.

Sharing hooks across a team

Point Git at a checked-in directory instead of .git/hooks:

git config core.hooksPath .githooks

Commit a .githooks/ directory with executable scripts, and every clone that runs this config line will use them — a lightweight alternative to a full hook manager for small teams.

Server-side hooks

Bare repositories (the kind Git hosts store) also support hooks that run on the server: pre-receive, update, and post-receive, used to reject pushes that don’t meet a policy or to trigger CI/deployment. GitHub and GitLab expose equivalent functionality through branch protection rules and CI webhooks rather than raw server-side hooks, since you don’t have shell access to their servers.