Skip to main content

Secrets in git history: finding them with gitleaks

Security · · 5 min read

Why "I already deleted it" does not work

Git is built so that a commit is never edited, only added. If a password landed in a file and was removed by the next commit, the file in the working tree is clean while the object holding the password stays in the history — and travels with every clone. Anyone holding a clone sees the secret with git log -p, without even knowing what to look for.

Hence the rule that saves hours: treat the secret as compromised first, deal with the history afterwards. The order is the opposite of intuition, but it is the only correct one — while you rewrite history, the old key keeps working.

Finding them: gitleaks

gitleaks reads the whole history and matches known key shapes against its rules. The latest release at the time of writing is 8.30.1, published on 21 March 2026. It has three modes of operation, and they are easy to confuse:

SubcommandWhat it looks atWhen you need it
gitleaks gitThe repository's commit historyThe main case: searching the past
gitleaks dirFiles on disk, no historyAn export, an archive, a directory without git
gitleaks stdinA stream on inputChecking a single file on the fly

If your version's help lists detect and protect instead of these, the binary is old — its syntax differs and the examples below will not fit.

gitleaks git --redact -v .
gitleaks git --redact --report-path /tmp/leaks.json --report-format json .
gitleaks dir --redact /var/www/example.com

The --redact flag in those examples is not decoration: without it a discovered password is printed in full and lands in the build log, which means it leaks a second time — now into the continuous integration system.

What to do with findings: rotation

Rotation is not "change the password" but closing every path the key opened:

  1. Issue a new secret and put it into service.
  2. Revoke the old one at the side that issued it: the payment console, the cloud provider, the mail service.
  3. Check the logs for use of the old secret — after revocation they show who was still trying to use it.
  4. If it was a database password, change it both in the application and in every service that talks to the same database.

Revocation matters more than the new password. A secret that was merely replaced and not revoked remains a working key in someone else's hands.

Rewriting the history

Only after rotation does it make sense to remove the object from the history. The tool for that is git filter-repo; the built-in filter-branch is deprecated and slower.

git filter-repo --invert-paths --path config/secrets.yml
# or a replacement by content:
printf 'literal:AKIAIOSFODNN7EXAMPLE==>REMOVED\n' > /tmp/replace.txt
git filter-repo --replace-text /tmp/replace.txt

Understand the consequences before running it: rewriting changes the identifiers of every affected commit. All clones and branches diverge from the new history, open merge requests break, and copies of the old objects remain with everyone who already cloned the repository. So rewriting removes the secret from future clones, but it does not cancel rotation.

Keeping it from happening again

A check before the commit costs seconds and catches exactly the case everything starts from:

cat > .git/hooks/pre-commit <<'SH'
#!/bin/sh
exec gitleaks git --redact --staged --no-banner
SH
chmod +x .git/hooks/pre-commit

The hook lives in .git and does not travel with the repository, so a fresh clone will not have it. If the whole team needs the check, it goes into continuous integration rather than resting on local hooks.

False positives and a baseline

On a large repository the first pass almost always yields findings that are not secrets: sample keys from documentation, examples in a readme, hashes. Deleting rules for the sake of silence is the wrong move — that switches the check off. The right approach is to record the known findings as a baseline and look only at what is new:

gitleaks git --redact --report-path .gitleaks-baseline.json --report-format json .
gitleaks git --redact --baseline-path .gitleaks-baseline.json .

The baseline deserves rereading from time to time: it is easy to leave a real secret in it, once declared "a test value".

What the tool will not find

gitleaks matches rules, which means it finds familiar shapes: cloud keys, service tokens, private keys. A password like Qwerty2026 in a settings file fits no pattern and stays unnoticed. So "the scanner is quiet" means "no known shapes were found", not "there are no secrets" — and that holds for any pattern-matching search.

What sits next to this

The same leak more often happens not through the history but directly: the settings file is served over HTTP. That is covered in exposed .git and .env in the web root. Where secrets should live instead of a file in the repository is in storing secrets on a server. Checking third-party dependencies is npm audit and composer audit.

Was this article helpful?
Back to Knowledge Base Ask Support