Git Hooks
In short
Git hooks are scripts that Git runs automatically at certain points, such as before a commit or a push, to check code, enforce rules, or automate tasks.
What are Git hooks?
Git hooks are scripts that Git runs automatically when specific events happen in a repository. For example, a pre-commit hook runs just before a commit is created, and a pre-push hook runs before changes are sent to a remote. They are commonly used to run linters, formatters, or quick tests so problems are caught before they reach anyone else.
Hooks live in the .git/hooks folder of each repository, and a hook is simply an executable file named after the event, such as pre-commit or commit-msg. If a hook like pre-commit exits with a non-zero status, Git stops the operation, so a failing check can block a bad commit. Because the .git folder is not part of the tracked project files, hooks are not shared when someone clones the repository; teams commit them to a regular folder and point Git to it with core.hooksPath, or use a hook manager tool.
Think of hooks as the checklist a pilot runs before takeoff: the flight only continues once every item passes. Client-side hooks run on a developer's machine, while server-side hooks such as pre-receive run on the Git server and can reject pushes that break team rules, such as commit messages without a ticket number.
Git hooks are sometimes confused with CI/CD pipelines. Local hooks give fast feedback, but any developer can skip them with git commit --no-verify, so they cannot guarantee quality on their own, which is why teams use hooks for speed and CI as the final gate. Git hooks are also different from webhooks, which are HTTP requests a hosting platform sends to other services when something happens in a repository.
Key takeaways
- Hooks are scripts that Git runs automatically on events like commit, merge, or push.
- A non-zero exit code from a hook like
pre-commitcancels the operation. - Hooks in
.git/hooksare not shared on clone, so teams usecore.hooksPathor a hook manager. - Client-side hooks can be skipped with
--no-verify, so CI remains the final check. - Server-side hooks such as
pre-receivecan enforce rules for everyone.
Example
#!/bin/sh
# .githooks/pre-commit: runs automatically before every commit
echo "Running linter and tests..."
npm run lint || exit 1 # a non-zero exit code blocks the commit
npm test || exit 1
# Enable this hooks folder for the repository (run once):
# chmod +x .githooks/pre-commit
# git config core.hooksPath .githooksReaders ask
How do I skip a Git hook?
Add --no-verify to the command, for example git commit --no-verify or git push --no-verify. Use it sparingly, because it bypasses the checks your team relies on.
How do I share Git hooks with my team?
Commit the hook scripts to a folder in the repository, such as .githooks, and have each developer run git config core.hooksPath .githooks. Many teams automate this with a hook manager that installs the hooks along with the project's dependencies.
What is the most common Git hook?
The pre-commit hook is the most widely used, typically to run formatters, linters, and fast tests on the files being committed.
See also
- GitVersion Control, p. 10Git is a free, open-source distributed version control system that tracks changes to files over time, so developers can collaborate and undo mistakes.
- CommitVersion Control, p. 5A commit is a saved snapshot of a project's files in Git, recorded with a unique ID, an author, a timestamp, and a message describing what changed.
- LintingTesting & Quality, p. 14Linting is the automated analysis of source code, without running it, to flag likely bugs, style problems, and suspicious patterns before the code ships.
- CI/CDDevOps & Cloud, p. 9CI/CD is a set of automated practices that build, test, and release code changes frequently, so software can be delivered to users quickly and safely.
- RepositoryVersion Control, p. 34A repository is the storage location for a project, holding all of its files plus the complete history of every change recorded by a version control system.
- WebhookBackend & APIs, p. 47A webhook is an automated HTTP request that one application sends to a URL you provide as soon as a specific event happens, such as a completed payment.
Spotted a mistake or something missing on this page?Suggest an edit