Git commit history is a graveyard of secrets that developers accidentally committed and then deleted. “Deleted” in Git doesn’t mean gone — it means hidden. This post covers every technique for extracting secrets from Git repositories, both remote and local.
Why Git Recon Works
When a developer commits an API key, then removes it in the next commit, both commits remain in the history indefinitely. Many developers believe git rm erases data permanently. It doesn’t — the original blob is still there, reachable via the commit hash.
The same applies to:
Hardcoded passwords committed to application code
Private keys checked in “temporarily”
.env files accidentally staged
Database connection strings in config files
CI/CD secrets embedded in pipeline configs
Automated Secret Scanning
Always run automated tools first — they cover far more patterns than manual search.
trufflehog (best for entropy + regex)
# Install pip install trufflehog3 # or brew install trufflehog
# Scan GitHub repo (includes full history) trufflehog github --repo=https://github.com/target/repo
# Scan local repo trufflehog git file:///path/to/repo
# Scan specific branch trufflehog git --branch=main file:///path/to/repo
# Only regex (no entropy) trufflehog git --no-entropy file:///path/to/repo
# JSON output for parsing trufflehog git file:///path/to/repo --json
gitleaks
# Install brew install gitleaks # or binary from github.com/gitleaks/gitleaks/releases
# Find the last commit that touched a file git log --all --full-history -- "path/to/deleted/file.env"
# Restore the file from that commit git show <commit_hash>:path/to/deleted/file.env
# Or checkout the file at that point git checkout <commit_hash> -- path/to/deleted/file.env
Stash entries
git stash list git stash show -p stash@{0} # show stash content git stash show -p stash@{1}
All branches and tags
# List all remote branches git branch -r
# Fetch all remote branches git fetch --all
# Checkout each and search for branch in $(git branch -r | grep -v HEAD); do echo"=== $branch ===" git log -p $branch | grep -iE "password|apikey|secret" -A 2 done
GitHub-Specific Recon
GitHub Search
Use GitHub’s code search to find secrets across public repos:
# Search for secrets mentioning your target "target.com""api_key" "target.com" password filename:.env org:target_org "BEGIN RSA PRIVATE KEY" "targetcorp"filename:*.pem "api.target.com""Authorization: Bearer"
# Reconstruct from pack files curl https://target.com/.git/objects/pack/pack-<hash>.idx curl https://target.com/.git/objects/pack/pack-<hash>.pack
CI/CD Secret Hunting
CI/CD configs frequently contain secrets:
# GitHub Actions cat .github/workflows/*.yml | grep -iE "secret|password|token|key" # Look for: ${{ secrets.XXX }} — these are referenced in CI but set in GitHub settings # Also look for hardcoded fallback values
No comments yet. Be the first.