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
- Rotate the secret at the provider. This is the step that makes you safe.
- Check the provider's logs for use you do not recognise.
- Remove the secret from the code and read it from the environment or a secrets manager.
- Decide whether to rewrite history. It is optional once the key is rotated.
- 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.