Safeguard
Security Guides•

How do you scan git history for leaked secrets?

Scan every commit on every branch, not just current files, then revoke what you find. Steps, what else to scan and the mistakes that waste time.

Safeguard Security Team
3 min read

To scan git history for secrets, run a secret scanner over every commit on every branch, not only the checked out files, then revoke each credential it finds. A scan of current files misses anything that was committed and later deleted, which is common.

Why history matters

Git keeps every committed version of every file. If a key was added in March and removed in April, a scan of today's files sees nothing, while anyone who clones the repository can still read the March commit. OWASP warns that a secret committed to code can persist in git history even after it is removed from current files.

GitHub documents its own secret scanning as covering the full git history on all branches, and as rescanning when new secret types are added. It also covers issue and pull request text, discussions, wikis and secret gists, which a plain file scan never sees.

What to scan

PlaceWhy it can hold a secret
All branches, including old onesStale branches keep old commits reachable
Tags and forks you controlTags pin old states
Issue and pull request textPeople paste logs and configs
CI logs and build artifactsDebug output can print variables
Container image layersBuild steps can bake values in

Steps

  1. Fetch all branches and tags so the scan has the full history.
  2. Run a scanner in full history mode, not diff mode.
  3. Sort findings into live credentials and fake or expired ones.
  4. Revoke and replace every live credential first. This is the step that closes the exposure.
  5. Decide separately whether to rewrite history. GitHub's guidance is that removing a secret from history is time intensive and often unnecessary once the credential is revoked. OWASP notes that rewriting history has side effects to weigh.
  6. Add a pre-commit check and a CI check so the same mistake is caught earlier next time.

Common mistakes

  • Rewriting history before revoking. The secret is already copied elsewhere.
  • Scanning only the default branch.
  • Treating a private repository as safe. See whether a secret in a private repo is a problem.
  • Ignoring the audit trail. Record who had access and when the key was last used.

How Safeguard helps

Safeguard provides SBOM and dependency scanning with reachability analysis, and policies, gates and guardrails you can apply before merge. You can try the free scan on a repository.

Quick check

Do I have to rewrite git history after a leak?

Not always. Revoking the credential is the essential step. Rewriting history is optional, has side effects, and does not help if the repository was already cloned.

Will scanning only the main branch find old secrets?

Only if they are reachable from main. Scan all branches and tags.

Sources

  • GitHub Docs, About secret scanning (fetched 19 October 2026).
  • OWASP Secrets Management Cheat Sheet (fetched 19 October 2026).

Last reviewed: 19 October 2026.

Never miss an update

Weekly insights on software supply chain security, delivered to your inbox.

Self-healing security runs on Safeguard.

Your first fix PR is minutes away.

No sales call required, even your agent can complete the purchase over MCP.