Secrets in Git History

A secret removed from HEAD still lives in every earlier commit, reflog, fork, clone, cache and (on GitHub) the public event archive, so deletion is never remediation; only revocation is.

How it works

Git is append-only: a commit that removes a secret adds a new object, it does not erase the old one. Copies survive in:

  • every prior commit on every branch and tag;
  • local reflogs and unpruned objects;
  • forks, collaborator clones, CI runner caches, backups, email notifications;
  • GitHub “oops commits” — force-pushed/deleted commits remain retrievable, and GH Archive records them as zero-commit PushEvents;
  • build artifacts made from the working tree rather than the index: a .gitignored settings.local.json still lands in Docker images (613 secrets, 99 valid, reached Docker Hub that way; one valid Cloudflare token appeared in 559 tags).

AI coding agents reproduce the human anti-pattern at scale: told to “remove the credential”, they commit a fix to HEAD and stop. A GitHub search for commit messages such as “redact exposed client secret” / “remove credentials from documentation” returned ~124,000 hits, many with timing signatures of agent-driven commits.

Techniques (finding it)

# working tree incl. uncommitted, then full history (gitleaks >= 8.19 syntax)
gitleaks dir . --redact -v
gitleaks git . --log-opts="--all" --redact -v --report-path gitleaks-report.json
 
# verified-only history scan
trufflehog git file://. --results=verified
 
# hunt by variable name, not by pasting a live value
git log -p --all -S 'AWS_SECRET_ACCESS_KEY'
  • Truffle Security’s Force Push Scanner mines GH Archive for dangling commits in an org/user; Sharon Brizinov’s scan of all oops commits since 2020 earned ~$25k in bounties, including an admin PAT over the Istio repos.
  • Mass history scans: 7.25M Gitee repos yielded 26,400 live credentials (Alibaba Cloud AccessKeys 9,473; YouTube 3,805; DeepSeek 3,170; OpenAI 1,348), ~3.64 per 1,000 repos; nearly half (~12,242) dated from 2025.
  • GitHub secret scanning covers all branches and full history by default once enabled.

Remediation order

  1. Revoke/rotate first. Treat the value as compromised the moment it was pushed; see Credential Rotation and Revocation.
  2. Scope: which repos/branches/tags/forks, which CI logs printed it, what the credential can do, who has clones.
  3. Only then, optionally, rewrite history:
    bfg --replace-text secrets.txt repo.git     # or git filter-repo
    git reflog expire --expire=now --all && git gc --prune=now --aggressive
    Coordinate the force-push, have every collaborator re-clone, and rotate again if the old value could still have been live during the rewrite. Rewrites break PRs, and forks keep the old objects.
  4. Prevent recurrence with pre-commit hooks and push protection (Secret Scanning).
BeliefReality
Delete the file and pushValue remains in prior commits
git filter-branch/BFG purgeForks, clones, caches, GH Archive retain it
Force-push over itGitHub keeps the dangling commit; it is scannable
Repo is private32.2% of internal repos hold a hardcoded secret vs 5.6% public; access changes, clones persist

Notable cases

  • CISA “Private-CISA” (2025–26) — contractor used a public repo to sync files between work and home machines; AWS GovCloud admin keys and a plaintext password CSV, with GitHub’s secret blocking deliberately disabled. Keys stayed valid ~48h after the repo came down.
  • tj-actions/changed-files (CVE-2025-30066) — secrets printed into workflow logs; CISA’s guidance was rotate, because values may appear even in obfuscated form. See tj-actions changed-files Compromise.
  • Rust Miri cache — Miri stored all env vars under target/; with GitHub Actions caching target/, secrets persisted into a cache readable by PRs (fixed to keep only CARGO_* and OUT_DIR).

Sources