Skip to main content

Git Hooks

Updated 2 min read

Share this page

Send the link, quote the definition with a link back, or show it as a card on your own site.

https://softwaredictionary.org/terms/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-commit cancels the operation.
  • Hooks in .git/hooks are not shared on clone, so teams use core.hooksPath or a hook manager.
  • Client-side hooks can be skipped with --no-verify, so CI remains the final check.
  • Server-side hooks such as pre-receive can enforce rules for everyone.

Example

A pre-commit hook that runs the linter and testsbash
#!/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 .githooks

Readers 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

Spotted a mistake or something missing on this page?Suggest an edit

Read a random page
Open today's review
Switch to the dark theme
Read this page in Türkçe

More

Settings