Git Hooks & Automation
7 pages · ~5 min✓ Reviewed
Builds on History Archaeology.
What Git Hooks Are
1. What a hook is
- Executable script Git runs at a fixed event: commit, push, receive
- Plain file, any language, starts with a shebang
- Lives in
.git/hooks/, filename = hook name, no extension
2. What Git runs
| File in .git/hooks | Git does |
|---|---|
pre-commit.sample | ignored, has an extension |
pre-commit, no chmod +x | ignored, silently |
pre-commit, executable | runs |
- Eventcommit
- Hook runsexit code
- Exit 0continues
- Non-zeroaborts
3. Example
#!/bin/sh npm test || exit 1 # chmod +x .git/hooks/pre-commit
.git/hooks/pre-commit blocks the commit if tests fail
4. Mistakes
- Hooks are local:
.git/hooksis never cloned or pushed - Teammates do not get your hooks
Any client hook that runs before the action and exits non-zero aborts it: pre-commit, prepare-commit-msg, commit-msg, pre-push. post-* hooks cannot abort.
Client-Side Hooks
1. Hooks that run before
- pre-commitlint staged
- prepare-commit-msgpre-fill
- commit-msgvalidate
- post-commitnotify
| Hook | Gets | Use |
|---|---|---|
pre-commit | nothing | lint, format, fast tests |
prepare-commit-msg | message file | pre-fill the editor text |
commit-msg | $1 = message file | validate the message |
pre-push | remote name + URL; refs on stdin | full test suite |
2. Validate the message
commit-msgreads the file at $1, exits non-zero to reject- Regex enforces Conventional Commits
Server-Side Hooks
1. The Three Hooks
| Hook | Runs | Can reject? |
|---|---|---|
pre-receive | once per push | whole push |
update | once per ref | that ref only |
post-receive | after accept | never |
2. Inputs and Use
- They run on the remote repository, not the client
pre-receivestdin: lines ofold-sha new-sha refname; non-zero rejects allupdateargs:refname old-sha new-shapost-receive: CI triggers, deploys, notifications
while read old new ref; do if [ "$ref" = refs/heads/main ]; then echo "main is locked"; exit 1 fi done
pre-receive: reject any push touching main
Sharing Hooks with core.hooksPath
1. Tracked Hooks Folder
git config core.hooksPath .githookspoints Git at a tracked folder- Used instead of
.git/hooks, so hooks are versioned with the repo - It is local config: each clone must run it once
- Put it in a setup script or a
maketarget
2. Local vs Global
| Scope | Command | Effect |
|---|---|---|
| Repo | git config core.hooksPath .githooks | This repo only |
| All repos | git config --global core.hooksPath <dir> | One path for every repo |
With --global, a repo's own local hooks stop running. Git reads only the hooksPath folder.
Tools: pre-commit and husky
1. pre-commit (Python framework)
- Config file: .pre-commit-config.yaml listing
repo,revand hookids - Run
pre-commit installonce to put the Git hook in place - Hooks run on staged files only by default
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
hooks:
- id: end-of-file-fixerrepo + pinned rev + hook ids
| Command | Effect |
|---|---|
pre-commit install | Enable the Git hook |
pre-commit run --all-files | Run on the whole repo |
pre-commit autoupdate | Bump pinned revs |
2. husky + lint-staged (Node)
- npx husky initcreates .husky/
- prepare scriptruns on install
- core.hooksPath.husky/_
- .husky/pre-commitnpx lint-staged
Skipping Hooks, Mistakes and Cheat Sheet
1. Skipping Hooks
| Command | Skips | Never skips |
|---|---|---|
git commit --no-verify (-n) | pre-commit, commit-msg | server-side hooks |
git push --no-verify | pre-push | server-side hooks |
HUSKY=0disables husky for that commandSKIP=hook-idskips named pre-commit hooks, keeps the rest
HUSKY=0 git commit -m "wip" SKIP=flake8 git commit -m "wip"
2. Common Mistakes
- Slow pre-commit (full test suite) pushes people to --no-verify
- Keep pre-commit fast; move heavy checks to pre-push and CI
- Client hooks are skippable: never rely on them for enforcement
Check yourself
A .git/hooks/pre-commit script ends with exit 1, yet commits still succeed. Likely cause?
- The file is not executable, so Git silently ignores it
- Fix:
chmod +x .git/hooks/pre-commit
A post-receive hook exits 1 after a push. Is the push rejected?
- No: the push was already accepted before the hook ran
- Only
pre-receiveandupdatecan reject
After git config core.hooksPath .githooks, what happens to .git/hooks/pre-commit?
- Git reads hooks from
.githooksinstead, so it stops running - The setting is local, so each clone must run it once
git commit --no-verify is used with a bad message. What is skipped, and what can still catch it?
- It skips
pre-commitandcommit-msg - Server-side hooks and CI still run, so repeat critical checks there