Security

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 -v

dir 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:1

and 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.json writes a report (JSON, CSV, JUnit or SARIF with --report-format).
  • --baseline-path takes an earlier report and lists only new findings, so you can adopt the tool on an old repository without fixing everything first.
  • A gitleaks:allow comment 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-update

TruffleHog 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=verified shows 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:

Valuegitleaks 8.30.1TruffleHog 3.99.8
ghp_ token in an old commitfound (github-pat)found (Github)
sb_secret_…found (generic-api-key)not reported
sb_publishable_…found (generic-api-key), though it is meant to be publicnot 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:

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 -v

In 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:

  1. 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.
  2. Check what the key was used for while it was exposed, in the provider's logs or billing, where it has them.
  3. 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.txt

To 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

FactValueSource
GitHub secret scanning on public repositoriesRuns automatically, freeScans the entire Git history on all branches.About secret scanning (opens in a new tab)Verified on
GitHub Secret Protection19 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 tested8.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 tested3.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

  1. gitleaks (opens in a new tab), gitleaks, accessed on
  2. TruffleHog (opens in a new tab), Truffle Security, accessed on
  3. About secret scanning (opens in a new tab), GitHub, accessed on
  4. About push protection (opens in a new tab), GitHub, accessed on
  5. About GitHub Advanced Security (opens in a new tab), GitHub, accessed on
  6. GitHub security plans (opens in a new tab), GitHub, accessed on
  7. Removing sensitive data from a repository (opens in a new tab), GitHub, accessed on
  8. Rotate a leaked or compromised API key (opens in a new tab), Supabase, accessed on
  9. Security: what OpsCrew sets up and checks (opens in a new tab), OpsCrew, accessed on
Edit this article
  • 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.

    6 min read

  • 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.

    9 min read

  • 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.

    11 min read

Put OpsCrew to work on your app.

Run npx opscrew init in your app's repository. It checks your tools and tokens first, for free.