Skip to content

Lesson 3 of 12 · 13m

Secrets in Git Never Die

A committed credential lives in every clone, fork, reflog, and pull-request ref, and deleting it adds a commit — it never removes the blob.

Commit a .env, realize the mistake an hour later, git rm .env && git commit && git push. The file is gone from HEAD. It is not gone. Git stores the file as a blob addressed by its SHA-1, and that blob is reachable from the old commit, which is reachable from history, the reflog, every clone ever made, every fork, and every cached copy on the forge — including GitHub's refs/pull/* namespace, which survives force-pushes and even branch deletion. You added a commit. You removed nothing.

What "deleted" actually means

The reachability goes further than git log shows. The blob survives in .git/objects until garbage collection, in the reflog for ninety days, in every stash, and in refs/pull/<n>/head on the forge if the commit ever passed through a pull request — GitHub keeps those refs even after the PR closes and the branch deletes, which is why their docs tell you to contact support to drop them. git gc --prune=now only cleans your local copy; the remote keeps its own object store.

History rewriting helps hygiene, not safety. git filter-repo --path .env --invert-paths or BFG Repo-Cleaner will strip the blob from your history, and GitHub's docs walk through scrubbing plus a support ticket to drop the cached PR refs. Do it — a clean repo is worth having. But the blob was replicated the moment you pushed: every CI checkout, every teammate's laptop, every mirror, every shallow clone a bot made in between. And if the repo was public for even minutes, the credential was scraped. GitHub exposes a public events firehose; scrapers regex it for AKIA[0-9A-Z]{16}, ghp_, -----BEGIN, Slack xoxb-, Stripe sk_live, and a hundred other shapes. AWS runs its own scanner against public commits and attaches the AWSCompromisedKeyQuarantineV2 managed policy to exposed IAM keys — in published tests this lands within minutes of the push. The quarantine denies the dangerous calls; it does not undo the exposure window.

The wrong mental model

"The repo is private, so it's fine." Private means unindexed, not unharvested. The repo lives on laptops, in CI caches, in forks nobody tracks, in backup snapshots, in the onboarding tarball someone mailed a contractor. Uber's 2016 breach ran exactly this path: credentials pulled from a private GitHub repo led to S3 and 57 million rider and driver records. The other half of the wrong model: "I'll clean the history and it's resolved." Scrubbing removes the artifact. It does not un-read the credential for everyone who already copied it. Rotation is the fix; cleaning is cosmetic.

What to actually do

  • The moment a secret is committed — public or private — treat it as public. Rotate first, scrub second. Revoke or rotate the credential before you open filter-repo.
  • Scrub anyway: bfg --delete-files .env or git filter-repo --path .env --invert-paths, force-push, and if it was on GitHub, open a support ticket to drop PR refs. Then tell everyone to re-clone; their old checkouts still carry it.
  • Prevent the class, not the instance: gitleaks protect --staged as a pre-commit hook and gitleaks git in CI catch the push before it happens. GitHub's push protection blocks pushes containing recognized secret formats — turn it on at the org level.
  • Scan what already exists: trufflehog git file://repo --only-verified (or --results=verified) tests each finding against the live API so you learn which old blobs are still valid credentials — those are the ones to rotate tonight, not eventually.

A commit is a broadcast with a permanent archive, and the archive outlives the mistake. Act like it — the only move that actually closes the hole is killing the credential, and every minute you spend scrubbing first is a minute the old one still works.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.