Skip to content
OQVIAN

How to stop API keys leaking into git

Add a scanner that runs before every commit and blocks any change that contains a key or password. Then scan the existing history, and rotate any key you find. Deleting a leaked key from the code is not enough: it stays in git history.

5 min read

Block it before the commit

A pre-commit hook runs a secret scanner such as gitleaks on the files being committed. If it finds something that looks like a key, the commit is refused and the developer sees which line caused it.

Run the same scanner in your deployment pipeline as well, because a hook on one laptop can be skipped.

Check what is already there

Scan the whole history once, not just the latest version. Keys removed months ago are still readable to anyone with access to the repository.

When a key has leaked

  • Revoke or rotate the key first. This is the step that actually protects you.
  • Check the provider's logs for use you do not recognise.
  • Move the new key into a secrets store or environment variable.
  • Rewrite the history only if the repository is public or widely shared.

See it in action

Our Security page shows an example log of a commit being blocked for a leaked key.

OQVIAN Security