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 pluscore.hooksPath(below) instead.
Commonly used hooks
| Hook | Runs | Typical use |
|---|---|---|
pre-commit | before a commit message is even prompted for | linting, formatting checks, running fast unit tests |
commit-msg | after the message is written, before the commit is created | enforcing a message format (e.g. Conventional Commits) |
post-commit | after a commit completes | notifications, triggering local build watchers |
pre-push | before git push sends objects | running the full test suite, blocking pushes to protected branches |
pre-rebase | before a rebase starts | blocking rebases of already-published branches |
post-checkout | after switch/checkout | reinstalling dependencies when they've changed |
post-merge | after a successful merge/pull | same, 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.