# 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 `PushEvent`s;
- build artifacts made from the **working tree** rather than the index: a `.gitignore`d `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)
```bash
# 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:
```bash
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]]).
| Belief | Reality |
|---|---|
| Delete the file and push | Value remains in prior commits |
| `git filter-branch`/BFG purge | Forks, clones, caches, GH Archive retain it |
| Force-push over it | GitHub keeps the dangling commit; it is scannable |
| Repo is private | 32.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`).
## Related
- [[Secrets Management]]
- [[Secret Scanning]] · [[Hardcoded Secrets]] · [[Credential Rotation and Revocation]]
- [[GitHub Actions Security]] · [[AI Coding Assistant Risks]]
## Sources
- [[raw/infoq.com/researcher-unearths-thousands-of-leaked-secrets-in-github-s-oops-commi-2770|Researcher Unearths Thousands of Leaked Secrets in GitHub's Oops Commits]]
- [[raw/peris.ai/26-400-working-keys-in-7-25m-repos-deleting-from-git-does-not-revoke-13287|26,400 working keys in 7.25M repos: deleting from Git does not revoke]]
- [[raw/mongodb.com/scanning-7-25-million-public-repositories-on-china-s-largest-git-platf-12421|Scanning 7.25 Million Public Repositories on China's Largest Git Platform]]
- [[raw/blakyaks.com/how-to-detect-and-clean-up-leaked-secrets-in-your-git-repositories-2771|How to Detect and Clean Up Leaked Secrets in Your Git Repositories]]
- [[raw/medium.com/find-leaked-secrets-in-git-and-rotate-them-before-they-are-used-14860|Find leaked secrets in Git and rotate them before they are used]]
- [[raw/blog.gitguardian.com/ai-is-the-newest-developer-to-misunderstand-secrets-in-your-git-histor-9920|AI Is the Newest Developer To Misunderstand Secrets in Your Git History]]
- [[raw/blog.gitguardian.com/claude-code-settings-local-json-secrets-17-months-of-exposure-on-githu-13955|Claude Code settings.local.json Secrets: 17 Months of Exposure]]
- [[raw/krebsonsecurity.com/cisa-admin-leaked-aws-govcloud-keys-on-github-8102|CISA Admin Leaked AWS GovCloud Keys on Github]]
- [[raw/blog.rust-lang.org/github-actions-leaking-secrets-when-miri-output-is-cached-12241|GitHub Actions leaking secrets when Miri output is cached]]
- [[raw/docs.github.com/about-secret-scanning-github-docs-4504|About secret scanning (GitHub Docs)]]
- [[raw/thehackernews.com/the-state-of-secrets-sprawl-2026-9-takeaways-for-cisos-3777|The State of Secrets Sprawl 2026: 9 Takeaways for CISOs]]
- [[raw/github.com/trufflehog-find-verify-and-analyze-leaked-credentials-2776|TruffleHog - Find, Verify, and Analyze Leaked Credentials]]