back to index

Git Key Guardian

Protect sensitive keys from accidentally being uploaded to your git history at any point.

repo: https://github.com/EricSpencer00/git-key-guardian


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.

Why this project

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.

Features

How it works

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:

  1. Grep-style regex checks using the patterns in patterns/common_patterns.txt, with comments and blank lines ignored.
  2. Fixed string checks against the personal keys file at $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.

Install

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

Usage

Test locally without installing

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.

Patterns and personal keys

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:

Exact personal secrets go in $HOME/.git-key-guardian/personal_keys.txt. Lines beginning with # are ignored.

Implementation notes

Caveats

Links