Ingenious AI

Guides · AI development

How to find and remove secrets in code

A secret committed to a repository is never really deleted by deleting the line: it lives in the history, in clones, in forks, in CI caches and in the laptop of everyone who ever pulled. That is why the correct response to a leaked key is not 'remove it' but 'rotate it, then clean'. This guide walks the full procedure — discovery across the whole history, the order of operations that actually closes the hole, and the prevention layer that stops the next one.

By the Ingenious AI team · Published 14 November 2026 · Reviewed 14 November 2026

The short answer

Find secrets in code by scanning the entire repository history (not just the current files) with a secret scanner tuned to your providers' key formats, then handle every hit in this order: rotate the credential first, check its usage logs for abuse, and only then clean the history. Prevention is a pre-commit hook plus CI scanning plus a real secrets manager — because a secret that was valid for one hour in a public (or widely cloned) repo must be treated as compromised forever, however thoroughly you scrub.

Try it free: Vibe Audit — scan your codebase for exposed secrets and the patterns that leak them — the first step of the cleanup described here.

What is a secret in code?

any credential — API key, password, token, private key, connection string — that has been written into source code, configuration, comments or test data. Once committed, a secret is a disclosure, not a storage mistake: version control is designed to preserve and distribute everything ever committed, which is the opposite of what a credential needs.

Scan the history, not just the tree

The current files are the least interesting place to look. Run a history-aware scanner over every commit, every branch, including deleted files and old configuration that 'nobody uses anymore'. Include the places secrets hide outside code: CI configuration, infrastructure files, notebooks, seed data, mobile app resources, and the test fixtures everyone assumes are fake — test fixtures have an uncanny habit of containing one real credential from the day someone debugged against production. Export the findings as a list you can work through: file, commit, secret type, and whether the credential is still valid.

Rotate first, clean second — always

For every valid secret found: rotate it (issue a replacement, deploy it, revoke the old one) before touching the repository. The reasoning is timing: history rewriting takes hours or days and touches every clone; rotation takes minutes and immediately ends the exposure. Then review the old credential's usage logs across the whole window since its first commit — anomalous IPs, unusual volumes, off-hours calls. Only after rotation and review do you rewrite history to purge the secret, knowing the purge is hygiene (limiting future discovery) rather than the fix itself.

Cleaning history without breaking the team

History rewriting (tools like git filter-repo) changes commit hashes, so it needs coordination: freeze merges, have everyone re-clone afterwards, and purge the secret from forks, mirrors and CI caches too. For public repositories, assume the secret was scraped within minutes of the push regardless of your cleanup — automated scrapers monitor public commits in near-real time — which is why rotation, not scrubbing, is the control that matters. Document what was done and when; an incident record turns an embarrassing find into an auditable response.

Prevention: hooks, gates, and a place secrets belong

Three layers stop the next leak. A pre-commit hook scans staged changes on the developer's machine, where the fix is cheapest. CI scanning blocks merges that slip past the hook. And a secrets manager gives credentials a legitimate home, so 'just for now in the config file' stops being the path of least resistance. Add the cultural rule: any secret found anywhere gets rotated without blame — teams that punish the finder get fewer findings and more silent leaks.

The AI-era wrinkle: generated code and shared chats

Modern leaks have new routes: AI assistants that hard-code a key from an example, developers pasting real credentials into a chat with a model for debugging, screenshots of consoles in tickets. Extend scanning to the places your team actually works — including AI-generated code (which reproduces secrets from its context) — and adopt the rule that credentials are never pasted into any chat, ticket or document. If a key has been in a prompt, it joins the rotate-first list automatically.

Your 5-action checklist

  1. Run a history-aware secret scan over all branches and file types, including fixtures, CI config and notebooks.
  2. For every valid secret: rotate it first, then review its usage logs for the full exposure window.
  3. Rewrite history to purge secrets only after rotation, coordinating re-clones and clearing forks and caches.
  4. Install the three prevention layers: pre-commit hook, CI gate, and a secrets manager as the legitimate home.
  5. Extend the rule everywhere: no credentials in chats, tickets, screenshots or AI prompts — ever.

Worked example: Twin Rivers Logistics

Twin Rivers Logistics, a fictional freight company, ran its first history scan expecting a clean result — the repo was private and the team was small. The scan found 14 secrets across 3 years, including a cloud key committed in the project's second week that was still valid and had console-level access, and a database password in a 'temporary' debug script that had survived four refactors. Rotation of all 14 took one day; usage review found no abuse, which the team could say with confidence because they checked before revoking. History cleanup took a coordinated weekend. The pre-commit hook installed afterwards has since blocked 6 attempted commits — each one a small, free save instead of a future incident.

Frequently asked questions

We deleted the file with the key. Are we safe?

No. The secret remains in the repository history and in every clone made while it existed. Deleting the file stops casual discovery by people browsing the current tree; it does nothing about the history. Rotate the credential — that is the action that makes you safe.

How fast are leaked keys found by attackers?

On public repositories, automated scanners detect new keys within minutes — sometimes seconds — of a push. Private repositories are safer but not safe: they leak through access changes, contractor laptops and accidental exposure. The safe assumption for any committed key is that it is known.

Do we need to rewrite git history after rotating?

It is good hygiene, especially to stop future accidental exposure and to satisfy auditors, but it is secondary. Rotation closes the risk; history rewriting reduces embarrassment and residual copies. Never let a slow history rewrite delay rotation.

What about secrets in environment files (.env) that aren't committed?

They are safer — until they are committed by accident, attached to a ticket, or included in a backup. Keep .env files out of version control via .gitignore, use a secrets manager for shared environments, and scan for .env files in commits as a specific check.

Can a scanner find every secret?

No scanner is perfect: custom token formats, split strings and encoded secrets can evade patterns. Combine scanning with provider-side usage monitoring (alerts on anomalous key use) so a missed leak still surfaces when someone tries to use it.

Related guides and tools