Frequently Asked Question

What are git hooks and what are they good for?

Git hooks are scripts that Git runs automatically at specific points in its workflow. They live in .git/hooks/ (local to each clone) or, with core.hooksPath, in a shared directory inside the repository. The script can be in any language as long as the file is executable; Git looks at the exit code and (for some hooks) stderr to decide whether to proceed. The most useful client-side hooks are pre-commit (run before a commit is recorded, typical home for linters, formatters, and "did you accidentally commit a debugger?" checks), commit-msg (validate the commit message format), and pre-push (run tests before pushing).

Server-side hooks like pre-receive and update run on the host and can reject pushes that violate policy, for example, refusing pushes to main that did not go through a pull request, or refusing commits whose messages do not reference a ticket ID. Because .git/hooks/ is not committed, projects usually distribute hooks via a tool like pre-commit (pre-commit.com) or lefthook, which install hooks from a configuration file checked into the repository.

Video

Further reading and video