Prompt
How do I detect secrets accidentally committed to GitHub?
Latest observation
To detect secrets accidentally committed to GitHub, use a mix of automatic scanning, history scanning, and preventive controls.
1) Scan your repo for secrets
Use purpose-built tools that look for common secret patterns.
Good options
- Gitleaks — popular, fast, easy to use
- TruffleHog — good for deep/history scanning
- GitHub Secret Scanning — built into GitHub for supported secret types
- detect-secrets — Yelp’s secret scanner, useful for baselining
Example: Gitleaks
gitleaks detect --source . --redact
Scan the full Git history too:
gitleaks detect --source . --log-opts="--all"
Example: TruffleHog
trufflehog git file://. --since-commit HEAD~100
Or scan the whole repo:
trufflehog git file://. --no-update
2) Check GitHub’s built-in alerts
GitHub can automatically detect secrets in:
- push events
- pull requests
- existing repositories (depending on plan and settings)
Look in:
- Repository → Security → Secret scanning
- Security alerts
If available, enable:
- Secret scanning
- Push protection
Push protection helps block commits before they land.
3) Search your commit history manually
If you suspect a leak, inspect all history:
git log -p
Search for likely patterns:
- API keys
- tokens
- private keys
.envvalues- connection strings
Useful commands:
git grep -n "API_KEY\|SECRET\|TOKEN" $(git rev-list --all)
Search for file types that often contain secrets:
.envconfig.jsoncredentialsid_rsa*.pem*.p12
4) Add pre-commit and CI checks
Prevent future leaks by scanning before code is merged.
Pre-commit hook example
Use pre-commit with secret scanners:
gitleaksdetect-secrets
CI/CD example
Run a scan on every push/PR:
gitleaks detect --source . --no-banner --redact
Fail the build if secrets are found.
5) If you find a secret, assume it’s compromised
Do this immediately:
- Revoke/rotate the secret
- Remove it from the repo
- Purge it from git history if necessary
- Invalidate caches/logs/artifacts that may contain it
- Audit access logs for misuse
To remove from history, use tools like:
git filter-repo- BFG Repo-Cleaner
Example with git filter-repo:
git filter-repo --path .env --invert-paths
Then force-push carefully:
git push --force --all
git push --force --tags
6) Best practices to reduce future risk
- Use environment variables, not hardcoded secrets
- Store secrets in a secrets manager:
- AWS Secrets Manager
- HashiCorp Vault
- Azure Key Vault
- Google Secret Manager
- Add
.envand similar files to.gitignore - Never commit private keys or tokens
- Rotate secrets regularly
- Limit secret scope and permissions
Quick recommendation
If you want the fastest practical setup:
- Enable GitHub Secret Scanning + Push Protection
- Add Gitleaks to CI and pre-commit
- Scan the full git history
- Rotate any exposed secrets immediately
If you want, I can give you:
- a one-command setup for Gitleaks,
- a GitHub Actions workflow to block secret commits,
- or a step-by-step cleanup plan for a repo that already leaked a secret.