Git Hooks Explained With Practical Examples
Git hooks are scripts that Git runs automatically when certain events happen, such as just before a commit is created or just after you merge. They let you attach your own checks and actions to the normal Git workflow: block a commit that contains leftover debugging code, enforce a message format, or run quick tests before code leaves your machine.
Hooks are simple in concept, and they are built into every Git repository. This article covers where they live, which ones matter most, and how to write and share them.
Where hooks live
Every repository has a hidden .git/hooks folder. When you create a repository, Git places sample files there, each ending in .sample. To activate a hook, create an executable file with the exact hook name and no extension, for example .git/hooks/pre-commit.
A hook can be written in any language that your system can run. Bash is common, but Python, Node or Ruby work equally well, as long as the file starts with a proper shebang line and has the execute permission.
chmod +x .git/hooks/pre-commit
The rule that makes hooks useful is simple: if a client side hook exits with a non zero status, Git cancels the action. Exit with zero, and it continues.
The most useful client side hooks
- pre-commit runs before Git asks for a commit message. It is the place for fast checks on the staged files.
- prepare-commit-msg runs before the message editor opens, and can pre-fill the message.
- commit-msg receives the path to the message file, so it can validate the message text.
- pre-push runs before a push, which suits slower checks such as a test suite.
- post-merge and post-checkout run after those actions, useful for refreshing dependencies.
Each hook receives context in its own way, through arguments or standard input. The sample files in the hooks folder show the details for each.
Example 1: block leftover debug statements
This pre-commit hook refuses a commit if any staged file contains a debugging marker.
#!/usr/bin/env bash
if git diff --cached | grep -nE '^\+.*(console\.log|debugger|FIXME_REMOVE)'; then
echo "Commit blocked: remove debug statements first." >&2
exit 1
fi
The command git diff --cached shows only what is staged, and the grep pattern looks at added lines, which start with a plus sign. If a match is found, the script prints the lines and exits with an error.
Example 2: enforce a commit message format
A commit-msg hook can require, for instance, a short summary line.
#!/usr/bin/env bash
first_line=$(head -n 1 "${1}")
if [ ${#first_line} -gt 72 ]; then
echo "Summary line is longer than 72 characters." >&2
exit 1
fi
if [ -z "$first_line" ]; then
echo "Commit message cannot be empty." >&2
exit 1
fi
The argument ${1} holds the path of the file where Git stored the draft message. The hook reads it, checks it and rejects the commit if it breaks the rule. Teams often use this to keep history consistent.
Example 3: run tests before pushing
#!/usr/bin/env bash
echo "Running tests before push..."
if ! python -m pytest -q; then
echo "Tests failed. Push cancelled." >&2
exit 1
fi
Slow checks belong in pre-push rather than pre-commit, because developers commit often and a slow hook is quickly bypassed or resented.
Keep hooks fast and predictable
A good hook finishes in a second or two. If it is slow, people will skip it. Some guidelines:
- Check only the staged files, not the entire project.
- Print a clear reason when blocking, and say how to fix it.
- Avoid network calls inside hooks when you can.
- Make the output short, so it is read.
- Never change files silently in a way that surprises the author. If a hook auto-formats code, make sure the changes are added to the commit visibly.
The sharing problem
The .git/hooks folder is not part of the repository, so it is not copied when someone clones the project. A hook you write there exists only on your machine. Two common solutions exist.
First, store hooks in a tracked folder such as .githooks, and tell Git to use it:
git config core.hooksPath .githooks
Each person must run that line once, so document it in your README or setup script. Second, use a hook management tool that reads a configuration file in the repository and installs hooks for you. Several open source options exist, and they handle running linters across many languages.
Hooks are not enforcement
Anyone can skip client side hooks by adding --no-verify to a commit or push, or by not installing them. Treat them as a helpful guide for developers, not as a security barrier. If a rule must be enforced, run the same check on the server, in a continuous integration pipeline or in a server side hook such as pre-receive. Use both: local hooks for fast feedback, and server checks as the final word.
Frequently Asked Questions
Can I skip a hook for a single commit?
Yes. Add --no-verify to git commit or git push. This is useful in emergencies, but if people use it constantly, the hook is probably too slow or too strict.
Do hooks work on Windows?
Yes. Git for Windows ships with a Bash shell that runs hook scripts. Keep line endings as LF in hook files, since Windows style line endings can break the shebang line.
Why is my hook not running?
Check that the file name matches the hook name exactly with no extension, that it has execute permission, and that its shebang line is correct. Also confirm that core.hooksPath is not pointing somewhere else.