Secrets in Git History: How to Find Them and What to Do Next
Secrets can linger in a repository’s history long after they disappear from the latest commit. When a .git directory is exposed, anyone can crawl back through every change and harvest credentials, API keys or configuration data.
How secrets slip into Git
Developers often work with local environment files that contain passwords, tokens or service endpoints. When an AI‑assisted coding session suggests inserting a key directly into source code, the line can be committed before anyone notices. The same risk applies to configuration files, build logs or temporary branches that are later merged without a thorough review. Once a secret is part of a commit, it becomes a permanent part of the project’s history.
Even disciplined teams can unintentionally expose data by running a repository initialization step inside the web root. This creates a publicly accessible .git directory if the web server does not block dot‑prefixed paths. Attackers can then request the HEAD, config or other internal files and retrieve the entire commit history, exposing everything that ever lived in the codebase.
- Environment files (.env) are common culprits
- AI code suggestions may embed keys directly
- Accidental init of a repository in a public folder
Why deleting a file isn’t enough
Git records every change as a snapshot. Removing a file in a later commit only adds a new snapshot that no longer contains the file, but the earlier snapshots remain intact. Those older snapshots are still reachable through the commit graph, and anyone with read access to the repository can retrieve the removed content.
If a secret was ever committed, the mere presence of that commit means the secret may have been copied, cached or logged by external services. Treating the secret as still valid creates a window for misuse. The safest approach is to assume the secret is compromised the moment it appears in any commit, regardless of later deletions.
Scanning the full history
Open‑source scanners such as Gitleaks and TruffleHog walk through every commit, searching for patterns that resemble keys, passwords or other sensitive strings. Running these tools against the entire history uncovers secrets that are hidden in older branches or squashed commits, providing a comprehensive view of exposure.
A full‑history scan should be part of a regular security hygiene routine, not just an after‑the‑fact reaction. Integrating the scan into CI pipelines ensures that new commits are checked immediately, while periodic deep scans catch any lingering artifacts that might have slipped through earlier checks.
- Use Gitleaks or TruffleHog for full‑history analysis
- Include the scan in continuous integration
Immediate response steps
When a secret is discovered, the first action is to treat it as compromised. Rotate or revoke the credential with the issuing service before doing anything else. This cuts off any potential misuse while you work on cleaning the repository.
After rotation, clean the repository history using tools like git‑filter‑repo or the BFG Repo‑Cleaner. These utilities rewrite the commit graph to excise the secret from every snapshot, then force‑push the rewritten history. Remember to inform any collaborators that they will need to re‑clone or reset their local copies to avoid reintroducing the old data.
- Revoke or rotate the exposed credential
- Rewrite history with git‑filter‑repo or BFG
Preventing future leaks
The most reliable defense is to stop secrets from entering the repository in the first place. Add .env and other sensitive files to .gitignore so they never get staged. Enforce a pre‑commit hook that runs a secrets scanner, blocking any commit that contains patterns matching known credential formats.
On hosted platforms, enable built‑in secret scanning and push protection. These services automatically detect accidental pushes of known secret patterns and reject them before they reach the remote repository. Combine these safeguards with a web‑server rule that denies access to any path beginning with a dot, preventing accidental exposure of the .git directory in production.
- Add sensitive files to .gitignore
- Run a pre‑commit secrets scan
- Enable platform secret scanning and push protection
- Block dot‑directories at the web server
What the documented sources report
In September 2026 an internal pilot by Klickify on one of its own web properties found that the site's .git directory was publicly readable in production: /.git/HEAD and /.git/config returned HTTP 200 with real content. An automated scan with nuclei (979 templates) and sqlmap came back clean; the exposure was found by manual reconnaissance. Checking the other sites in the same 12-site portfolio showed the same exposure on all 12. The cause was a deploy template that deliberately runs git init in each new site's web root as a rollback safety net, without a matching web-server rule blocking dot-directories. The fix was an nginx rule denying paths that start with a dot, verified against all 12 public URLs: /.git/HEAD returned 404 afterwards while the homepages stayed at 200. A full-history gitleaks scan of all 12 repositories found no real secrets; two matches were false positives that looked like domain names. How long the exposure lasted is unknown.
Source: Klickify CyberSecurity Agency internal incident record (September 29, 2026)
Common questions
Can I recover a secret after it has been rotated?
Rotating replaces the old value, but the old secret may still exist in logs or backups. Treat the original as compromised and remove it from all accessible locations.
Do I need to rewrite history for every secret found?
If a secret was ever committed, rewriting removes it from the public record. However, if the credential has already been revoked, a history rewrite is optional but still recommended for hygiene.
What if my CI pipeline already builds from a compromised commit?
Stop the pipeline, revoke the credential, clean the history, then restart the build with the sanitized repository. Ensure the pipeline pulls the updated, rewritten history.
How often should I run full‑history scans?
Run them regularly as part of a security audit cycle and after any major merge or repository migration to catch lingering secrets.
Will blocking dot‑directories affect legitimate Git operations?
No, the block only applies to HTTP requests for paths that start with a dot. Local Git commands continue to function normally.
Sources
- Klickify CyberSecurity Agency internal incident record (September 29, 2026)