# What a 12-line pre-commit hook catches, and what it doesn't

Published: 2026-11-28
Updated: 2026-12-01

Rights: original
Source: https://kairos-2z6s.onrender.com/res/pre-commit-secrets-hook-en
A pre-commit hook is the cheapest place to stop a credential from leaving a laptop, and this one is 12 lines of POSIX shell with no dependencies. It was tested against 22 cases before publishing. Here is exactly what it does, and where it stops.

Disclosure: this site shows ads that support the free content. The article text has no monetized links.

```sh
#!/bin/sh
# .githooks/pre-commit
names=$(git diff --cached --name-only --diff-filter=ACM)
if printf '%s\n' "$names" | grep -qE '\.env|\.pem$|\.key$|id_rsa'; then
  printf '%s\n' "$names" | grep -E '\.env|\.pem$|\.key$|id_rsa' | sed 's/^/blocked file: /'
  exit 1
fi
if git diff --cached -U0 | grep -E '^\+' | grep -v '^+++' | grep -inE "(api[_-]?key|secret|passw(or)?d|token)[[:space:]]*[:=][[:space:]]*[\"']?[^[:space:]\"'.()]{12,}"; then
  echo "blocked: possible secret in staged diff"
  exit 1
fi
exit 0```

Install: `git config core.hooksPath .githooks`, then `chmod +x .githooks/pre-commit`. If a hooks path is already set globally, check it with `git config --show-origin --get core.hooksPath` first.

Blocked in testing: `prod.env`, `.env.local`, `.envrc`, `notes.env.md`, `.env.example`, `API_KEY = "..."` in uppercase, an unquoted `SECRET=...` line, `token: ghp_...` in YAML, `apiKey = "..."` in camelCase, `password = "Tr0ub4dor&3xyz1234"`, and a file named `my notes.env`. (Test: 22 cases run before publishing; 12 blocked, 2 of them false positives, and 10 passed, 1 of them a cleanup commit. The full list is above.)

Let through by design: `password = "shortpw12"` (10 characters), `password = process.env.PASSWORD`, `password = getPassword()`, and removing a leaked secret.

Missed (the 5 known misses in the list above were tested: JSON style, a bare `sk-...` key, `secret_key_base`, and a secret cut short at `.` or `(`; the rest are by design): JSON style `{"api_key": "..."}`, a bare `sk-...` key, `secret_key_base = "..."`, secrets under 12 characters, secrets split across lines, base64 blobs, keys already in history, a `DB_PASS=` line, and filenames containing newlines. A real secret containing `.`, `(`, `)` or a quote inside is cut short at that character, so a value like `abc.defghijklmnop` is missed.

Two false positives to expect: `.env.example` is blocked, because the name check matches any path containing `.env`; and a long variable name or long plain words after the key name are blocked too, for example `password = input_password_value`. `.env.example` is the one most people do commit, so rename it or narrow the pattern.

## The 22 cases

1. file `prod.env`: blocked
2. file `.env.local`: blocked
3. file `.envrc`: blocked
4. file `notes.env.md`: blocked
5. file `.env.example`: blocked, a false positive
6. file `my notes.env`: blocked
7. `a1.txt` containing `API_KEY = "abcdefghijklmnop"`: blocked
8. `a2.txt` containing `SECRET=abcdefghijklmnop`: blocked
9. `a5.yml` containing `token: ghp_abcdefghijklmnopqrst`: blocked
10. `a6.js` containing `apiKey = "12345678901234567890"`: blocked
11. `s.py` containing `password = "Tr0ub4dor&3xyz1234"`: blocked
12. `fp2.py` containing `password = input_password_value`: blocked, a false positive
13. file `environment.md`: passed
14. `a3.txt` containing `password = "shortpw12"`: passed, by design
15. `a4.js` containing `password = process.env.PASSWORD`: passed, by design
16. `a7.py` containing `password = getPassword()`: passed, by design
17. `n.json` containing `{"api_key": "abcdefghijklmnop"}`: passed, a known miss
18. `o.txt` containing `sk-abcdefghijklmnopqrstuvwx`: passed, a known miss
19. `p.rb` containing `secret_key_base = "abcdefghijklmnop"`: passed, a known miss
20. `d1.txt` containing `password = abc.defghijklmnop`: passed, a known miss
21. `d2.txt` containing `password = abcdefghij(klmnop`: passed, a known miss
22. cleanup commit removing a committed secret ("leak.py"): passed, removing a secret is not blocked

For anything past this, gitleaks or trufflehog in CI, and a `git log -p` sweep for keys already in history.

## Related guides
- [Storing API keys in a Python project](https://kairos-2z6s.onrender.com/res/python-store-api-keys-env-vars-en)
- [Leaked API key: revoke first, then scan history](https://kairos-2z6s.onrender.com/res/leaked-key-revoke-and-scan-en)
- [Removing a committed secret from all Git history](https://kairos-2z6s.onrender.com/res/remove-secret-from-git-history-en)
- [Deleting .env in a new commit does not hide the key](https://kairos-2z6s.onrender.com/res/deleted-env-still-in-history-en)
- [Python any and all: test a whole list](https://kairos-2z6s.onrender.com/res/python-any-all-full-en)
- [Pipes and redirection: where command output goes](https://kairos-2z6s.onrender.com/res/pipes-redirection-full-en)
- [Git Merge vs Rebase: How Each Integrates Changes and Shapes History](https://kairos-2z6s.onrender.com/res/merge-vs-rebase-full-en)
- [Git still tracks .env after adding it to .gitignore](https://kairos-2z6s.onrender.com/res/gitignore-env-not-ignored-en)
