Guide · Pre-commit
Block commits that contain secrets
One pasted key in a staged env file is enough. A pre-commit check looks at what you are about to commit and stops it before it reaches your history.
In shortRun lk scan --install-hook once in the repository. From then on, git commit runs lk scan --staged and refuses the commit if an added env file line holds a stored value or something that looks secret. The output names the file and line, never the value.
Why secrets get committed
Git keeps out an env file only when your ignore rules name it. A new file name, such as .env.staging, or a file added with git add -f goes straight in. Once a key is in history, deleting the line later does not remove it from earlier commits.
A check at commit time is the cheapest place to catch this. It runs on your Mac, before anything is pushed.
Set up the check
-
Scan the folder you are in
Run the scan once by hand to see what it reports.
Terminal lk scanIt looks for env files under the current folder:
.env,.env.*and*.env. It skips examples, templates, backups, and anything innode_modules, build or.gitfolders. Each finding is printed asFILE:LINE NAMEfollowed by the reason, never a value:Terminal (example) .env.staging:3 STRIPE_KEY holds the value of shop/staging/STRIPE_KEYThere are two reasons. The value equals an item you have stored, or it looks secret: a private key, a connection string with credentials, a token with a known prefix such as
sk_live_orghp_, or a value of 8 or more characters under a name like*_TOKEN,*_SECRETor*_API_KEY. Identifiers, hosts andlatchkey://references are not findings. -
Move what it found into Keychain
Import the file and rewrite it, so the value is replaced by a reference.
Terminal lk import .env.staging --rewriteLines it moved are now references, so they no longer count. Keep API keys out of your .env files covers the import in full.
-
Install the hook
Terminal lk scan --install-hookThis writes a pre-commit hook that runs
latchkey scan --staged. It honourscore.hooksPathand works in worktrees. If the repository already has a hook that is not Latchkey's, it is left alone. Add--forceto replace it. -
Commit as usual
--stagedreads only the lines a commit would add, from the Git index, in env files that are staged. If it finds a plain value, the commit is refused and the file, line and variable name are given, plus the stored item when the value equals one. Fix the file and commit again. To skip the check for one commit, usegit commit --no-verify.
Use the same check in a script
lk scan exits 1 when it finds anything, so you can call it from your own script. --json prints place, key, reason (stored or looks-secret) and items for each finding.
lk scan --jsonThe comparison with stored items asks for approval every time. Grants and agent-safe settings do not stand in for it, and agents cannot ask. If Latchkey can't be reached or you don't approve the comparison, values that look secret are still reported, and the exit status is 1, so the hook refuses the commit even when nothing looks secret.
What this doesn't do
- It reads env files only. A key pasted into source code, a README or a config file is not checked.
- The "looks secret" test is a heuristic. A secret with an unusual shape under an innocent name can pass.
- Stored values shorter than 8 characters, and items saved as plain values, are left out of the comparison with Keychain.
- If neither
lknorlatchkeyis on the PATH, the hook says so and lets the commit through. git commit --no-verifyskips it, as it skips any hook.
The CLI reference lists every option, and the security page explains where protection ends.