Sub topic 10
Verifying the secret is gone
This chapter assumes you have already rotated the credential and run git filter-repo in a fresh clone, using --invert-paths, --replace-text, or both. It also assumes you know basic git log and git grep. Verification comes after the rewrite and before the force-push. A push publishes the result, so any mistake you catch first costs you one re-run instead of a second round of cleanup on the host.
Why a separate verification step
git filter-repo reports success when it finishes rewriting. That tells you the rewrite ran. It does not tell you the rules matched everything. Common causes of a silent miss:
- The secret appeared in a file you did not list, such as a copy in
docs/or a test fixture. - The secret appeared in several forms, such as quoted, URL-encoded, or split across a line break.
- The secret appeared in a commit message or an annotated tag message, which
--replace-textdoes not touch. - A regex in the expressions file was slightly wrong and matched nothing.
Verification means searching the rewritten repository for the secret in every place Git stores text. Do all the searches below from the cleaned clone, not from your original working copy. The original still contains the secret.
What to search
Git keeps text in four places that matter here. Each needs its own search.
| Where the secret could be | What it covers | Command |
|---|---|---|
| Changes in commits | Any commit where the count of the string changed | `git log --all --oneline -S'...'` |
| File contents at each commit | Every file in every snapshot | `git grep` over `git rev-list --all` |
| Commit messages | Subject and body | `git log --all --grep='...'` |
| Tag messages | Annotated tags | `git for-each-ref` on `refs/tags` |
You should also confirm that removed paths are gone from every commit.
Search the changes
The pickaxe search from the locating chapter is the first check. Run it again, exactly as you did before the rewrite:
git log --all --oneline -S'AKIAEXAMPLEKEY12345'Before the rewrite this listed the commits that added or removed the string. After a successful rewrite it prints nothing.
-S matches a literal string. To catch variants of a credential by its shape, use -G, which takes a regular expression and matches against the changed lines:
git log --all --oneline -G'AKIA[0-9A-Z]{16}'This finds any AWS-style access key ID, not only the one you knew about. A hit here can reveal a second leaked credential that you also need to rotate.
Search the file contents
-S and -G look at diffs. A string that exists in a snapshot but was never part of a change cannot be missed by them in practice. Even so, search the snapshots directly. This is the strongest check, because it reads every file as it exists in every commit:
git rev-list --all | while read -r commit; do
git grep -I -q -F -e 'AKIAEXAMPLEKEY12345' "$commit" && echo "found in $commit"
done
echo "search finished"The loop prints one line for each commit whose snapshot contains the string. If only search finished appears, no commit contains it. The one-line form from the topic plan, git grep -I 'AKIA...' $(git rev-list --all), works on small repositories. On a large repository the expanded command line can exceed the operating system's argument limit, which is why the loop is safer.
The options matter. -F treats the pattern as a fixed string, so dots and brackets in a password do not act as regex syntax. -I skips binary files. A secret inside a binary file, such as a compiled artifact or a database file, will not be found with -I. If the repository holds binaries that might contain it, replace -I with -a, which searches them as text.
Search the messages
git log --all --format='%H %s' --fixed-strings --grep='AKIAEXAMPLEKEY12345'This searches commit messages on every ref. If it prints anything, run git filter-repo --replace-message ../expressions.txt as described in the previous chapter, then repeat all the checks.
Annotated tags carry their own message, and git log --grep does not read it. Check it separately:
git for-each-ref --format='%(refname) %(contents)' refs/tags | grep -F -e 'AKIAEXAMPLEKEY12345'Confirm removed paths are gone
If you used --invert-paths, check that the path no longer appears in any commit:
git log --all --oneline -- config/secrets.envEmpty output means no commit on any ref touches that path. Also look at the file's other possible names. If someone renamed secrets.env to .env.old at some point, the original path rule may not have followed it. The analysis files from git filter-repo --analyze list paths that ever existed, and you can search that list for similar names.
Put the checks in one script
Typing the real secret into a shell command stores it in your shell history and shows it in the process list while the command runs. A script that reads it from the keyboard avoids that. Save this as verify-secret.sh outside the repository, and run it from inside the cleaned clone:
#!/usr/bin/env bash
set -u
read -r -s -p 'Secret to search for: ' SECRET
echo
[ -n "$SECRET" ] || { echo 'No secret entered.'; exit 2; }
found=0
if [ -n "$(git log --all --oneline -S"$SECRET")" ]; then
echo 'FOUND: a commit changes the string (git log -S)'
found=1
fi
while read -r commit; do
if git grep -I -q -F -e "$SECRET" "$commit"; then
echo "FOUND: file contents at commit $commit"
found=1
fi
done < <(git rev-list --all)
if [ -n "$(git log --all --format=%H --fixed-strings --grep="$SECRET")" ]; then
echo 'FOUND: a commit message contains the string'
found=1
fi
if git for-each-ref --format='%(contents)' refs/tags | grep -F -q -e "$SECRET"; then
echo 'FOUND: a tag message contains the string'
found=1
fi
if [ "$found" -eq 0 ]; then
echo 'Clean: no matches in any ref.'
fi
exit "$found"Run it with bash verify-secret.sh. The exit status is 0 when clean and 1 when any check found something, so you can reuse the script in a pre-push routine. Run it once for each secret you removed.
Check that the old objects are gone
Searching refs shows the secret is unreachable from any branch or tag. The old commits may still sit in the object database, where they are unreachable but not yet deleted. git filter-repo expires the reflog and runs garbage collection when it finishes, so the old objects are normally removed. You can confirm this with one old commit hash from before the rewrite. If you saved the pre-rewrite hash of an affected commit, test it:
git cat-file -e 3f2a9c1d && echo 'old commit still present' || echo 'old commit absent'Replace 3f2a9c1d with a real hash from your notes. The map of old to new hashes is stored in .git/filter-repo/commit-map, which is useful for looking up the old hash of any commit. absent is the result you want. If the commit is still present, run git reflog expire --expire=now --all and then git gc --prune=now --aggressive, and test again.
Never publish the .git/filter-repo directory. It holds the hash map and analysis reports, and the reports can mention the removed file names.
Cross-check with a scanner
Manual searches find what you thought to look for. A secret scanner finds patterns you did not think of. Tools such as gitleaks and trufflehog walk the full history and flag strings that look like credentials. Run one over the cleaned clone as a last check, and over the old history first if you want to see what it catches.
Expect some false positives, such as example keys in documentation. Judge each hit. A hit that is a real second credential means rotating it too.
Read the result and decide
If a check finds a match, do not patch the already-rewritten clone with another commit. A new commit that deletes the string leaves it in the history again. Go back to a fresh clone of the original repository, correct the rules, and run the whole rewrite again. Running filter-repo repeatedly on an already-rewritten clone also works, but starting fresh keeps the process easy to repeat and to explain.
Common mistakes
- Searching in the wrong directory. Searching your original clone always finds the secret and can mislead you into thinking the rewrite failed.
- Using
-Swith a regex.-Sis literal. Use-Gfor patterns. - Forgetting
--all. Without it,git logsearches only the current branch, and a secret on another branch or tag goes unnoticed. - Reading
git grepwithout-Fwhen the secret contains characters like+,., or$, which a regex would treat specially. - Stopping after one clean search. The four searches check different places.
Exercise
You ran git log --all --oneline -S'hunter2pass' on the rewritten clone and it printed nothing. Name two other checks you still need before force-pushing, and say what each covers that the first does not.
Show answer
Search file contents at every commit (git rev-list --all with git grep -I -F), which reads the snapshots directly instead of the diffs. Search commit messages with git log --all --fixed-strings --grep, which covers text -S does not examine. Checking annotated tag messages and removed paths are also valid answers.
Exercise
A verification run reports FOUND: a commit message contains the string. A teammate suggests making a new commit with an amended message and pushing it. Why does that fail, and what should you do instead?
Show answer
A new commit does not alter the existing commit, so the old message stays in history. Run git filter-repo --replace-message with an expressions file from a fresh clone, or add the rule to the original rewrite and redo it. Then repeat every verification check on the result.