Guides

How to find secrets in a git repository

Updated 2026-11-17

Short answer

To find secrets in a git repository, scan both the current files and the whole commit history for the patterns of known credentials: cloud keys, tokens, private keys and passwords. When you find one, rotate it first and remove it second, because a secret that was pushed must be treated as exposed.

Where secrets hide

  • Configuration files and .env files committed by accident.
  • Test files and example code with a real key pasted in.
  • Old commits: deleting the line does not remove it from history.
  • CI configuration and infrastructure files.
  • Notebooks and scripts.

What to do when you find one

  1. Rotate the secret at the provider. This is the step that makes you safe.
  2. Check the provider's logs for use you do not recognise.
  3. Remove the secret from the code and read it from the environment or a secrets manager.
  4. Decide whether to rewrite history. It is optional once the key is rotated.
  5. Add a scan to pull requests so the next one is caught before it merges.

How a scanner checks a secret without using it

A careful scanner does not try the key against the provider. It checks the structure of the token, its built-in checksum and its expiry, and compares private keys with public lists of compromised keys. scancodebase works this way and always shows the secret masked.

More guides

Scan yours before the next merge.