Identity & AI agent security
Finding and Rotating Secrets Leaked into Git History
A secret deleted in a later commit is still leaked. Scan the whole git history, rotate in the right order, check for use, and know when a rewrite helps.
Deleting a committed API key in the next commit does not remove it. The key is still in the repository's history, in every clone, in every fork, and on the hosting provider's servers. Finding secrets leaked into git history and rotating them is an incident response task with a fixed order: find every copy, rotate the credential, check whether it was used, and only then decide whether rewriting history is worth the disruption. This guide walks through each step with commands, and explains why the order matters.
Why is a deleted secret still a live leak?
Git stores every version of every file. A commit that removes a key from
config.py adds a new version of the file; the old version, with the key, is
still an object in the repository that anyone with a clone can read with
git log -p or git show. On GitHub the commit also stays reachable through
any pull request that included it, through forks, and directly by its SHA.
So the question to ask is not "is the secret in the current code?" but "was the secret ever pushed anywhere?". If it was, treat it as exposed to everyone who had read access to the repository at any point since, plus everyone they shared it with.
How do you scan the whole history?
Scan a full mirror, not your working copy. A normal clone only has the branches you fetched, and a shallow clone (common in CI) has almost no history at all:
git clone --mirror https://github.com/example/payments-api.git
cd payments-api.gitGitleaks scans git history with its
git subcommand. Pass --log-opts to include every ref, and redact the
matches in the output so the report does not become a second copy of the
leak:
gitleaks git -v --redact --log-opts="--all" \
--report-format json --report-path leaks.json .Gitleaks' maintainer has declared it feature complete, with future releases limited to security fixes, and pointed users to a successor project. It still works as shown, but check the project page before you build a pipeline around it.
TruffleHog takes a different approach: for many credential types it calls the provider to check whether the credential still works.
trufflehog git file://. --results=verified,unknown --json > trufflehog.jsonBe clear about what that means. Verification sends your leaked credential to its provider from wherever the scan runs. That is usually what you want, but it is an outbound call carrying a secret, and some teams need to approve it first.
How do you triage what you found?
For each finding, answer four questions before touching anything.
Is it real? Test fixtures, documentation examples and revoked keys show
up in every scan. A key in a testdata/ directory from a provider's own
documentation is not an incident. A key with a valid checksum in a deployment
script is.
Is it still valid? If your scanner verified it, you know. If not, check with the provider's own "who am I" call from a controlled machine, or assume it is live.
Who introduced it, and when? The commit tells you the window of exposure and the person who knows where else the credential was used:
git log --all --format='%h %an %ad %s' -S "$(cat needle.txt)"Put the secret, or a unique part of it, in a file rather than on the command line, so it does not land in your shell history.
What can it reach? A read-only token for one repository and an AWS key with administrator access are different incidents. Look up the credential's permissions before you decide how fast to move and who to tell.
What order should rotation happen in?
Rotate before you rewrite history, and replace before you revoke.
- Issue a new credential with the same access, or less if the old one was over-privileged. This is the moment to fix that.
- Deploy the new credential to everything that used the old one, through your secret store rather than a config file.
- Verify the services are using the new credential.
- Revoke the old credential. Revoking first is faster but causes an outage in every service you did not know about. If the credential is being actively abused, accept the outage and revoke immediately.
- Check for use during the exposure window.
For an AWS access key on a machine user:
aws iam create-access-key --user-name payments-deployer
# deploy the new key, confirm it is in use, then:
aws iam update-access-key --user-name payments-deployer \
--access-key-id AKIAOLDKEYEXAMPLE --status Inactive
aws iam delete-access-key --user-name payments-deployer \
--access-key-id AKIAOLDKEYEXAMPLEMaking the key inactive before deleting it gives you a quick way back if something breaks, while still stopping the leaked key from working.
How do you check whether a leaked key was used?
Use the provider's audit log, filtered by the credential. For AWS:
aws iam get-access-key-last-used --access-key-id AKIAOLDKEYEXAMPLE
aws cloudtrail lookup-events \
--lookup-attributes AttributeKey=AccessKeyId,AttributeValue=AKIAOLDKEYEXAMPLE \
--start-time 2026-09-01T00:00:00ZLook for calls from source IP addresses or user agents your systems do not
use, and for calls made between the leak and the rotation. lookup-events
covers the last 90 days of management events in one region, so run it for
each region the key could reach, or query the trail in your log store for
longer windows. GitHub, GitLab and most SaaS providers have an equivalent
audit log keyed on the token. If you find use you cannot explain, this has
become a breach investigation, not a cleanup.
The AWS key ID in a leak is only half the credential. If your scanner found the key ID but not its secret, search the same commit and nearby files for the secret before you conclude the leak was harmless.
Should you rewrite git history?
Sometimes, and never instead of rotating. Rewriting removes the secret from the repository going forward. It does not remove it from existing clones, forks, CI caches or anyone's laptop. GitHub's own guidance says it will only help purge sensitive data where rotating the credential cannot mitigate the risk.
Rewriting is worth it when the data cannot be rotated: personal data, a private key whose certificate is pinned somewhere you do not control, or a file that should never have been public. For those, use git filter-repo 2.47 or later in a fresh clone:
# replace the literal secret everywhere it appears
echo 'sk_live_EXAMPLEKEYVALUE==>REDACTED' > ../replacements.txt
git filter-repo --sensitive-data-removal --replace-text ../replacements.txt
# or remove a file from all history
git filter-repo --sensitive-data-removal --invert-paths --path config/prod.env--sensitive-data-removal fetches all refs first, so data in refs outside
branches and tags is rewritten too, and it reports the first changed commit
and which pull request refs were affected. Push with
git push --force --mirror origin, then on GitHub contact Support with that
report so they can dereference the affected pull requests and run garbage
collection. Every collaborator has to re-clone, because a single git pull
followed by git push from an old clone puts the secret back.
The cost is real: commit SHAs change, commit signatures are dropped, open pull requests break and closed ones lose their diffs. Coordinate it like a migration.
How do you stop the next one?
Catch secrets before they are committed. Gitleaks ships a pre-commit hook:
repos:
- repo: https://github.com/gitleaks/gitleaks
rev: v8.30.1 # pin to a release you have reviewed
hooks:
- id: gitleaksTurn on your Git host's push protection as well, because local hooks are
opt-in and easy to skip with --no-verify. Neither replaces a scheduled
history scan: hooks only see new commits, and the leaks you have not found yet
are already in the history.
The longer-term fix is fewer static secrets to leak. A CI job that gets short-lived credentials through OIDC federation has nothing to commit by mistake, which is the subject of least privilege for CI/CD identities.
How does TrustIvan find and track leaked secrets?
TrustIvan's secret scanning walks a repository's full git history, so a credential deleted in a later commit is still reported, and it refuses shallow clones by default rather than report a partial scan as clean. Each finding names the commit that first introduced the secret and that commit's author. It runs 189 detectors: 12 of its own (AWS and Google Cloud keys, GitHub, GitLab, Slack, Stripe, npm and PyPI tokens, PEM private keys, JWTs, database URLs with embedded passwords, and a generic high-entropy rule, with checksums verified where the format has one, such as GitHub's) plus 177 adopted from gitleaks' default configuration. Detection is line by line, so a credential split across lines is not found.
Validity checking is off by default and an owner or admin turns it on. When
enabled, 16 detectors across 14 providers, among them GitHub, GitLab, npm,
Slack, Stripe, OpenAI and Anthropic, call a fixed, read-only "who am I"
endpoint and store only the verdict. Those checks are tested against
documented response shapes rather than live providers, and paired
credentials such as AWS keys, private keys and database URLs are not
checked. Recording a rotation (POST /api/v1/findings/:id/rotate)
keeps the finding open, because the secret is still in history, and adds a
timeline event so a rotated credential does not look unaddressed.
trustivan secrets scan --staged runs the same detectors locally against
staged lines with no network call, as a pre-commit hook.
For machine credentials NHI Security manages, rotation is a staged, validated workflow rather than a note; see credentials and rotation and the exposed credential use case.