How to scan a git repository for leaked secrets
Scan a repository's full history for leaked keys with gitleaks and TruffleHog in Docker, add a pre-commit hook, know what GitHub's secret scanning covers, and clean up after a leak: rotate first, then rewrite history.
TL;DR
- Scan the history, not just the files: a key deleted in a later commit is still in every clone, and only a history scan finds it.
- gitleaks:
docker run --rm -v "$PWD:/repo" zricethezav/gitleaks:latest git /repo --redact -v; TruffleHog:docker run --rm -v "$PWD:/repo" trufflesecurity/trufflehog:latest git file:///repo. - GitHub (opens in a new tab)'s secret scanning is free and on by default for public repositories; private repositories in an organisation need GitHub Secret Protection, which only Team and Enterprise plans can buy.
- After a leak, rotate or revoke the key first; rewriting history with git-filter-repo comes second, and only removes the copies you control.
On this page
To scan a git (opens in a new tab) repository for leaked secrets, run a secret scanner over its whole history, not just the files you see today. Two free, open-source scanners do this well and run in Docker (opens in a new tab) without installing anything: gitleaks (gitleaks git) and TruffleHog (trufflehog git). A secret scan is one of the first items on the production checklist for a vibe-coded app.
Everything below was run on 11 November 2026 with gitleaks 8.30.1, TruffleHog 3.99.8 and git-filter-repo 2.47.0 in Docker containers, against a throwaway repository with a fake token.
Why the history matters
If a key went into a commit and a later commit removed it, the file looks clean, but the first commit still contains the key, and so does every clone, fork and backup of the repository. Anyone who runs git log -p can read it. AI coding tools make this easy to do by accident: a key pasted into a chat often lands in a config file, gets committed, and is "fixed" in the next commit. Both tools below read all commits on all branches by default in their git mode.
Make a test repository with a fake key
Try the tools on something harmless first. This builds a token that has the shape of a GitHub personal access token, ghp_ followed by 36 letters and digits, but starts with FAKE and is random, so it is nobody's real key:
mkdir leak-demo && cd leak-demo
git init -b main
fake="ghp_FAKE$(head -c 64 /dev/urandom | base64 | tr -dc 'A-Za-z0-9' | head -c 32)"
printf 'export const githubToken = "%s";\n' "$fake" > config.js
git add config.js && git commit -m "Add config"
printf 'export const githubToken = process.env.GITHUB_TOKEN;\n' > config.js
git commit -am "Read the token from the environment"The working tree is now clean: config.js reads the token from the environment. The first commit still holds the fake token.
Scan a git repo for secrets with gitleaks
gitleaks has three modes: git for the history, dir for files on disk and stdin. Run both of the first two from the repository's folder:
docker run --rm -v "$PWD:/repo" zricethezav/gitleaks:latest dir /repo --redact -v
docker run --rm -v "$PWD:/repo" zricethezav/gitleaks:latest git /repo --redact -vdir reported no leaks found and exited with 0. git scanned both commits and found the token:
INF 2 commits scanned.
WRN leaks found: 1
Finding: ...onst githubToken = "REDACTED";
Secret: REDACTED
RuleID: github-pat
File: config.js
Line: 1
Commit: ee25e4e9658ebacd77040326953f8584d956f572
Fingerprint: ee25e4e9658ebacd77040326953f8584d956f572:config.js:github-pat:1and exited with 1, which is what a CI job needs to fail. --redact keeps the secret out of the output; --redact=60 shows the first part of it, enough to tell keys apart. Other options worth knowing:
--log-opts="--all commitA..commitB"limits the scan to a range of commits, for example only what a pull request adds.--report-path report.jsonwrites a report (JSON, CSV, JUnit or SARIF with--report-format).--baseline-pathtakes an earlier report and lists only new findings, so you can adopt the tool on an old repository without fixing everything first.- A
gitleaks:allowcomment on a line, or a finding's fingerprint in.gitleaksignore, silences a false positive.
gitleaks's README now says the tool is feature complete, that future releases will be security patches only, and that its maintainer is shifting focus to a new project, Betterleaks. gitleaks still works as shown; check the README before you build a long-term setup on it.
Scan with TruffleHog
docker run --rm -v "$PWD:/repo" trufflesecurity/trufflehog:latest git file:///repo --no-updateTruffleHog found the same token in the same commit, and labelled it:
Found unverified result
Detector Type: Github
Raw result: ghp_FAKE…
Rotation_guide: https://howtorotate.com/docs/tutorials/github/
Token_type: Personal Access Token (classic)
Commit: ee25e4e9658ebacd77040326953f8584d956f572
File: config.js
Line: 1"Unverified" comes from TruffleHog's distinctive feature: for each key it recognises, it calls the provider's API to see whether the key works. A verified result is a live key and the most urgent thing on your list; our fake token failed that test. Two consequences:
- Verification sends found keys to their providers. If you do not want that, add
--no-verification. --results=verifiedshows only live keys. With it, our scan reported nothing. Useful for triage, but do not use it as your only scan: a key that fails today, because the provider was down or the key was suspended, can work again tomorrow.
With --fail, TruffleHog exits with 183 when it finds something (with --no-verification --fail, our scan exited with 183), so it can fail a CI job too. TruffleHog prints the full secret in its output, so be careful where that output goes.
What each found
The same .env file, with made-up values in the shapes of a Supabase (opens in a new tab) secret key, a Supabase publishable key and a Stripe live secret key, scanned with dir and filesystem:
| Value | gitleaks 8.30.1 | TruffleHog 3.99.8 |
|---|---|---|
ghp_ token in an old commit | found (github-pat) | found (Github) |
sb_secret_… | found (generic-api-key) | not reported |
sb_publishable_… | found (generic-api-key), though it is meant to be public | not reported |
sk_live_… | found (stripe-access-token) | found (Stripe) |
Real keys may match rules that made-up values do not, so treat this as an illustration, not a benchmark. Scanners find the formats they know: run two, and add your own search.
Your own search with plain Git
git grep can search every commit at once. Put in the prefixes your providers use:
git grep -nE 'ghp_|sb_secret_|service_role|sk_live_' $(git rev-list --all)In the test repository it printed the old commit, the file and the line. git log --all --oneline -S 'ghp_' lists the commits that added or removed the string. On a large repository, git rev-list --all produces a long argument list, so prefer the scanners there.
GitHub's secret scanning and push protection
If your repository is on GitHub, some of this already runs for you:
- Public repositories: secret scanning runs automatically and for free over the entire history of all branches, and alerts appear in the repository's Security and quality tab. For keys from GitHub's partners, GitHub notifies the provider, which can revoke the key.
- Private and internal repositories in an organisation: secret scanning needs GitHub Secret Protection, which you can buy only on the GitHub Team or Enterprise plans. It cost 19 US dollars per active committer per month on 11 November 2026.
- Push protection blocks a push that contains a supported secret before it lands. For your personal account it is on by default and covers pushes to public repositories. Turned on for a repository, it is free on public repositories and needs Secret Protection on private and internal ones. By default anyone with write access can bypass a block by giving a reason, and each bypass creates an alert.
GitHub detects the patterns it supports. For a private repository owned by a personal account, or by an organisation without Secret Protection, none of this runs, so a local scan is the only check you have.
Stop the next leak
A pre-commit hook. This hook runs gitleaks on what you are about to commit, and stops the commit if it finds a secret. Save it as .git/hooks/pre-commit and make it executable (chmod +x); it needs gitleaks installed, which its README offers through Homebrew, Docker, Go or a release binary:
#!/bin/sh
exec gitleaks git --pre-commit --staged --redact --no-banner -vIn the test, a commit of a file with a fake token printed the finding and stopped with exit code 1; nothing was committed. To skip the hook when you must, commit with git commit --no-verify. gitleaks's README also shows the same check for the pre-commit framework, where SKIP=gitleaks git commit … skips it instead.
Keep env files out of Git. Add .env and .env.* to .gitignore, with !.env.example if you commit an example with empty values. Many templates ignore only .env, so a .env.production slips through.
Keep secrets out of the frontend. A key in a VITE_ or NEXT_PUBLIC_ variable is in every visitor's browser even if it never touches Git. Environment variables in production explains which values end up there.
After a leak: rotate first
If a scan finds a real key, the order is:
- Rotate or revoke the key at the provider, and move your servers to the new one. GitHub's guide makes this the first step and says it may be enough on its own. Once the old key is dead, the copies in Git are harmless. Supabase's guide adds: fix the cause before you rotate, or the new key can leak the same way. The Supabase anon key and RLS article covers rotating a Supabase secret key.
- Check what the key was used for while it was exposed, in the provider's logs or billing, where it has them.
- Rewrite history, if the value must go too. GitHub recommends git-filter-repo. To replace the value everywhere, list it in a file as
old-value==>REMOVED-SECRET, one per line, then run:
git filter-repo --sensitive-data-removal --replace-text ../replacements.txtTo remove a whole file from every commit instead: git filter-repo --sensitive-data-removal --invert-paths --path config.js. In the test, the first commit's config.js read githubToken = "REMOVED-SECRET" afterwards and gitleaks found no leaks. Then git push --force --mirror origin replaces the remote's history.
Know the costs before you rewrite: every commit hash after the first changed one changes, closed pull requests lose their diffs, and everyone with a clone must re-clone or clean theirs, or the key comes back with their next push. Forks keep the old commits, and GitHub can still serve them from cached views and pull requests; GitHub Support can remove those in some cases. That is why rotation comes first: you cannot guarantee that every copy is gone.
Doing this with OpsCrew
OpsCrew's free npx opscrew scan checks a repository for leaked secrets, exposed Supabase or Firebase (opens in a new tab) data and risky configuration, and flags an env file that Git does not ignore. Each finding comes with the file and line, and the report goes to opscrew/SECURITY_SCAN.md. See what OpsCrew checks. When a credential OpsCrew made may have leaked, npx opscrew rotate replaces it everywhere OpsCrew put it: see rotating your app's credentials.
Next on the checklist
A clean scan is one item on the production checklist for a vibe-coded app. The next ones are usually Row Level Security for a Supabase backend and environment variables that work in production.
Key facts
| Fact | Value | Source |
|---|---|---|
| GitHub secret scanning on public repositories | Runs automatically, freeScans the entire Git history on all branches. | About secret scanning (opens in a new tab)Verified on |
| GitHub Secret Protection | 19 USD per active committer per monthFor private and internal repositories; requires a GitHub Team or Enterprise plan. | GitHub security plans (opens in a new tab)Verified on |
| gitleaks version tested | 8.30.1The README says gitleaks is feature complete and future releases will be security patches only. | gitleaks on GitHub (opens in a new tab)Verified on |
| TruffleHog version tested | 3.99.8With --fail, exit code 183 means results were found. | TruffleHog on GitHub (opens in a new tab)Verified on |
Frequently asked questions
How do I scan a GitHub repo for secrets?
For a public repository, GitHub's secret scanning already runs for free. For any repository, clone it and run gitleaks or TruffleHog against the clone; TruffleHog can also scan a GitHub repository or organisation directly with its github subcommand.
Is GitHub push protection free?
On public repositories, yes: it is on by default for your personal account's pushes and free to turn on for a repository. For private and internal repositories in an organisation it needs GitHub Secret Protection, which requires a Team or Enterprise plan and costs 19 US dollars per active committer per month (November 2026).
Sources
- gitleaks (opens in a new tab)
- TruffleHog (opens in a new tab)
- About secret scanning (opens in a new tab)
- About push protection (opens in a new tab)
- About GitHub Advanced Security (opens in a new tab)
- GitHub security plans (opens in a new tab)
- Removing sensitive data from a repository (opens in a new tab)
- Rotate a leaked or compromised API key (opens in a new tab)
- Security: what OpsCrew sets up and checks (opens in a new tab)
Related articles
- Security
Automatic security updates on Ubuntu with unattended-upgrades
Ubuntu Server installs security updates by itself every day. How to check that yours really does, what it installs and what it doesn't, when to reboot, and how to change the time, all tested on Ubuntu 24.04.
- Security
Docker ignores your UFW firewall. Here is how to fix it
A port published with Docker answers from the internet even when ufw denies it. We reproduced it on Ubuntu 24.04 with Docker 29 and tested the fixes Docker documents: 127.0.0.1, the provider's firewall and DOCKER-USER.
- Security
How to harden an Ubuntu 24.04 server for production
An ordered checklist for a fresh Ubuntu 24.04 VPS that runs a production app: SSH keys, firewalls Docker can't bypass, fail2ban, updates, Docker, secrets, backups and monitoring. Tested on 24.04.