Git Key Guardian is a pre-commit hook and helper toolkit. It scans staged changes for common secret patterns and for a user-supplied list of personal keys, so that API keys, tokens, and credentials are caught before they enter git history.
Secrets get committed by accident. API keys, SSH keys, cloud credentials, and other sensitive strings can slip into commits or CI logs. Git Key Guardian is an opt-in check that runs locally as a shared hook and reports matches against a configurable set of regex patterns and a personal key list.
The tool is deliberately simple and conservative. It scans only staged changes, uses maintainable regex patterns, and supports exact-string matches for specific keys.
patterns/common_patterns.txt, one per line, with inline comments allowed.$HOME/.git-key-guardian/personal_keys.txt.core.hooksPath so it covers all local repositories.On git commit, the pre-commit hook captures the staged diff with zero context and extracts newly added lines, meaning those starting with a single +. It then runs two checks:
patterns/common_patterns.txt, with comments and blank lines ignored.$HOME/.git-key-guardian/personal_keys.txt.If either check matches, the hook prints a sample of the matching lines and prompts for abort or continue.
Clone the repository and run the installer script. The installer copies the pre-commit hook to a shared hooks directory and configures git to use it globally.
git clone https://github.com/EricSpencer00/git-key-guardian.git
cd git-key-guardian
chmod +x ./scripts/install.sh
./scripts/install.sh
Patterns are copied to $HOME/.git-key-guardian/patterns/common_patterns.txt and the hook is installed at $HOME/.git-key-guardian/hooks/pre-commit. The installer also creates an editable personal_keys.txt.
To uninstall, remove the shared hooks directory or run:
git config --global --unset core.hooksPath
git add.git commit. The hook scans staged changes automatically.To exercise the hook without changing the global git configuration, create a temporary repository and run a commit, as described in CONTRIBUTING.md:
mkdir /tmp/gkg-test && cd /tmp/gkg-test && git init -q
cat > test.txt <<'EOS'
ess kay _ live_1234567890abcdefghijklmn
not_a_key AKIAABCDEFGHIJKLMNOP
random text
EOS
git add test.txt
GIT_DIR=.git GIT_WORK_TREE=. git commit -m "test" || true
The hook reports any matches and prompts before the commit proceeds.
The shipped regex list lives at patterns/common_patterns.txt. Entries follow three rules: one regex per line, with no /.../ delimiters; inline comments after whitespace and a # are allowed; and patterns should avoid catastrophic backtracking, preferring anchored subpatterns and bounded repetition.
Patterns included by default:
sk_live_[0-9a-zA-Z]{24} (Stripe live keys)sk-[A-Za-z0-9]{48} (older OpenAI key format)AKIA[0-9A-Z]{16} (AWS Access Key ID)ssh-rsa\s+[A-Za-z0-9+/=]+ (SSH public keys)Exact personal secrets go in $HOME/.git-key-guardian/personal_keys.txt. Lines beginning with # are ignored.
grep -En -f runs against staged additions.grep -Fn -f, again after comment and blank lines are removed.git diff --cached --unified=0 plus an awk filter for ^+[^+]).scripts/install.shhooks/pre-commitpatterns/common_patterns.txt