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

  1. Scan the folder you are in

    Run the scan once by hand to see what it reports.

    Terminal
    lk scan

    It looks for env files under the current folder: .env, .env.* and *.env. It skips examples, templates, backups, and anything in node_modules, build or .git folders. Each finding is printed as FILE:LINE NAME followed by the reason, never a value:

    Terminal (example)
    .env.staging:3  STRIPE_KEY  holds the value of shop/staging/STRIPE_KEY

    There 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_ or ghp_, or a value of 8 or more characters under a name like *_TOKEN, *_SECRET or *_API_KEY. Identifiers, hosts and latchkey:// references are not findings.

  2. 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 --rewrite

    Lines it moved are now references, so they no longer count. Keep API keys out of your .env files covers the import in full.

  3. Install the hook

    Terminal
    lk scan --install-hook

    This writes a pre-commit hook that runs latchkey scan --staged. It honours core.hooksPath and works in worktrees. If the repository already has a hook that is not Latchkey's, it is left alone. Add --force to replace it.

  4. Commit as usual

    --staged reads 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, use git 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.

Terminal
lk scan --json

The 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

The CLI reference lists every option, and the security page explains where protection ends.

Related