Git Hooks
Hooks are scripts Git runs automatically at certain moments: before a commit is created, after a checkout, before a push. They let you enforce rules (no debug code, a message format), run quick tests, or automate chores.
Hooks live in .git/hooks/. A hook is an executable file named exactly after the event, such as pre-commit. If a "pre-" hook exits with a non-zero status, Git aborts the operation.
mkdir app && cd app && git init -q
ls .git/hooksapplypatch-msg.sample
commit-msg.sample
post-update.sample
pre-applypatch.sample
pre-commit.sample
pre-merge-commit.sample
pre-push.sample
pre-rebase.sample
pre-receive.sample
prepare-commit-msg.sample
push-to-checkout.sample
sendemail-validate.sample
update.sample
The .sample files are disabled examples; remove the .sample suffix (and make the file executable) to activate one.
Common hooks
| Hook | Runs | Can stop the action? | Typical use |
|---|---|---|---|
pre-commit |
before the commit message is asked for | yes | lint, format check, block secrets/debug code |
prepare-commit-msg |
before the editor opens | yes | pre-fill the message (e.g. ticket number from branch name) |
commit-msg |
after the message is written | yes | enforce a message convention |
post-commit |
after the commit is created | no | notifications |
pre-push |
before pushing | yes | run tests |
post-checkout, post-merge |
after switching / merging | no | reinstall dependencies |
pre-receive, update, post-receive |
on the server during a push | pre-receive/update: yes | enforce policy, trigger deploys |
A pre-commit hook
This hook refuses commits whose staged changes contain console.log or a private key:
mkdir app && cd app && git init -q
cat > .git/hooks/pre-commit <<'EOF'
#!/bin/bash
if git diff --cached | grep -E '^\+.*(console\.log|BEGIN PRIVATE KEY)' >/dev/null; then
echo "pre-commit: remove console.log / private keys before committing:"
git diff --cached -U0 | grep -E '^\+.*(console\.log|BEGIN PRIVATE KEY)'
exit 1
fi
EOF
chmod +x .git/hooks/pre-commit
printf 'function pay(total) {\n console.log("debug", total);\n return charge(total);\n}\n' > pay.js
git add pay.js
git commit -m "Add payment"
echo "commit exit status: $?"
sed -i '/console.log/d' pay.js && git add pay.js
git commit -q -m "Add payment" && echo "committed: $(git log --oneline -1)"pre-commit: remove console.log / private keys before committing:
+ console.log("debug", total);
commit exit status: 1
committed: cea0fec Add payment
The hook checks the staged changes (git diff --cached), which is what will actually be committed.
A commit-msg hook
commit-msg receives the path of a file containing the message as its first argument. This one enforces Conventional Commits prefixes:
mkdir app && cd app && git init -q
cat > .git/hooks/commit-msg <<'EOF'
#!/bin/bash
if ! head -1 "$1" | grep -qE '^(feat|fix|docs|refactor|test|chore)(\(.+\))?: .+'; then
echo "commit-msg: use 'type: summary', e.g. 'fix: handle empty cart'"
exit 1
fi
EOF
chmod +x .git/hooks/commit-msg
echo "x" > a.txt && git add a.txt
git commit -q -m "stuff"
git commit -q -m "feat(cart): show item count" && git log --onelinecommit-msg: use 'type: summary', e.g. 'fix: handle empty cart'
1114d05 feat(cart): show item count
Skipping hooks
git commit --no-verify (or -n) skips pre-commit and commit-msg; git push --no-verify skips pre-push. That is why local hooks are a convenience, not security: anyone can bypass them. Real enforcement belongs on the server or in CI.
mkdir app && cd app && git init -q
printf '#!/bin/bash\necho "pre-commit: blocked"\nexit 1\n' > .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit
echo "x" > a.txt && git add a.txt
git commit -q -m "Try"
git commit -q --no-verify -m "Bypass the hook" && git log --onelinepre-commit: blocked
bf7e883 Bypass the hook
Sharing hooks with the team
.git/hooks is not part of the repository, so hooks aren't cloned. Two common solutions:
- Commit them in a folder such as
.githooks/and have everyone rungit config core.hooksPath .githooksonce. - Use a hook manager: pre-commit (Python), Husky (JavaScript), lefthook (any language). They install the hooks for you from a config file in the repository.
mkdir app && cd app && git init -q
mkdir .githooks
printf '#!/bin/bash\necho "shared hook: running checks"\n' > .githooks/pre-commit
chmod +x .githooks/pre-commit
git config core.hooksPath .githooks
echo "x" > a.txt && git add . && git commit -q -m "Add shared hooks" && git log --oneline
git ls-filesshared hook: running checks
4cd4c21 Add shared hooks
.githooks/pre-commit
a.txt
Test yourself
Where are a repository's local hooks stored by default?
- .githooks/
- .git/hooks/
- ~/.gitconfig
- /etc/git/hooks
What happens when a pre-commit hook exits with status 1?
- The commit is created with a warning
- The commit is aborted
- The hook is disabled
- Git retries
Why can't local hooks be relied on to enforce rules?
- They only run on Windows
- They are not cloned and can be skipped with --no-verify
- They run after the push
- They are too slow