TExcelTAD
Home › Git › Git Hooks

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.

bash · try it yourself
mkdir app && cd app && git init -q
ls .git/hooks
applypatch-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:

bash · try it yourself
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:

bash · try it yourself
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 --oneline
commit-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.

bash · try it yourself
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 --oneline
pre-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:

  1. Commit them in a folder such as .githooks/ and have everyone run git config core.hooksPath .githooks once.
  2. 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.
bash · try it yourself
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-files
shared hook: running checks
4cd4c21 Add shared hooks
.githooks/pre-commit
a.txt

Test yourself

Where are a repository's local hooks stored by default?

  1. .githooks/
  2. .git/hooks/
  3. ~/.gitconfig
  4. /etc/git/hooks

What happens when a pre-commit hook exits with status 1?

  1. The commit is created with a warning
  2. The commit is aborted
  3. The hook is disabled
  4. Git retries

Why can't local hooks be relied on to enforce rules?

  1. They only run on Windows
  2. They are not cloned and can be skipped with --no-verify
  3. They run after the push
  4. They are too slow