To remove secrets from git history, first revoke or rotate the secret, because old clones and forks keep it. Then find every leak with gitleaks or TruffleHog, rewrite history with git filter-repo using --sensitive-data-removal, force-push, and have everyone re-clone. A delete commit is not enough: the secret stays in every earlier commit. Check Terraform state, plan and .tfvars files too.
- Rotate or revoke the secret first. Rewriting history does not reach copies that already exist.
- gitleaks and TruffleHog scan every commit, not only the current files.
- git filter-repo is the tool the git project recommends; git filter-branch is not recommended.
- Terraform state and plan files hold secrets in plain text and belong outside git.
Why does deleting the file not remove the secret?
Git keeps every version of every file. When you delete a file that held a password and commit the change, the new commit no longer has the file, but every earlier commit still does. Anyone with a clone can check out an old commit, or search the history, and read the password.
The data can also live outside your repository. GitHub warns that "if you only rewrite your history and force push it, the commits with sensitive data may still be accessible elsewhere": in clones, in forks, in cached views and in pull requests that point at the old commits (GitHub Docs). Removing a secret from history is a cleanup job. It does not undo the exposure.
What should you do first when a secret is committed?
Rotate it. A secret is a credential one machine uses to prove itself to another: an API key, a database password, a private key, a cloud access key. GitHub's guidance is direct: if the data is a secret, "then as a first step you need to revoke and/or rotate that secret" (GitHub Docs). Revoking makes the old value useless. Rotating replaces it with a new value that was never committed.
Rotation does not end the job. OWASP says secrets that have been revoked or rotated "must be removed from the exposed system immediately, including secrets discovered in code or logs" (OWASP Secrets Management Cheat Sheet). An old password can still teach an attacker your naming pattern, and a key that was rotated in one system may still work in another.
- Revoke or rotate the credential at its source: the cloud console, the database, the vendor dashboard.
- Check the access logs of that service for use of the old value since the commit date.
- Write down which secret, which repository, which commit and when. You will need it for the rewrite and for the incident record.
How do gitleaks and TruffleHog find secrets in git history?
Both tools read the whole history, not just the files you see today. gitleaks is "a tool for detecting secrets like passwords, API keys, and tokens in git repos, files, and whatever else you wanna throw at it via stdin". Under the hood, it "uses the git log -p command to scan patches", so a key added in 2019 and deleted a week later is still found. Its README now says the project is feature complete and future releases will be security patches only.
TruffleHog says it "classifies over 800 secret types", and that for every secret it can classify "it can also log in to confirm if that secret is live or not". That check tells you which findings are still a present danger. TruffleHog also scans chats, wikis and logs, not only Git.
| gitleaks | TruffleHog | |
|---|---|---|
| How it reads history | git log -p over the commits you choose | Clones the repository and scans its history |
| Finds | Passwords, API keys and tokens by pattern rules | Over 800 classified secret types |
| Checks if a secret still works | No | Yes, for secrets it can classify |
| Good for | Pre-commit hooks and baselines on large repositories | Sorting live secrets from dead ones |
# gitleaks: scan every patch in a range of commits (gitleaks README)
gitleaks git -v --log-opts="--all commitA..commitB" path_to_repo
# gitleaks: save the findings as a report you can later use as a baseline
gitleaks git --report-path gitleaks-report.json
# TruffleHog: scan a local clone, run from the parent directory (TruffleHog README)
trufflehog git file://infra-repo --results=verified,unknown
Run both if you can. Different rule sets catch different strings. Scan every branch and tag, not only main.
How do you remove a secret with git filter-repo?
Use git filter-repo. The project describes itself as a tool to quickly rewrite git repository history, and says it "is now recommended by the git project instead of git filter-branch" (git filter-repo). The git manual itself says of filter-branch that "its use is not recommended. Please use an alternative history filtering tool such as git filter-repo" (Git manual).
GitHub's procedure needs a version with the --sensitive-data-removal flag, "meaning at least version 2.47" (GitHub Docs). There are two ways to use it. Remove whole files with --invert-paths and --path, or replace strings with --replace-text and a file that lists them, one per line. Each line is literal text unless it starts with regex: or glob:, and matches become ***REMOVED*** by default (git filter-repo manual).
- If the file was ever moved or renamed, add an extra --path for each old path. GitHub says you must.
- Close or merge open pull requests first. The rewrite changes "the hashes of the commits that introduced the sensitive data and all commits that came after".
- After the push, every collaborator re-clones. GitHub warns that "one merge commit could reintroduce some or all of the tainted history".
- On GitHub, contact support to remove cached views and pull request references.
# Requires git-filter-repo 2.47 or later. Run in a fresh clone. (GitHub Docs)
# Remove one file from every branch, tag and ref
git-filter-repo --sensitive-data-removal --invert-paths --path PATH-TO-YOUR-FILE-WITH-SENSITIVE-DATA
# Or replace each string listed in a file, everywhere in history
git-filter-repo --sensitive-data-removal --replace-text ../passwords.txt
# Overwrite the remote with the rewritten history
git push --force --mirror origin
old-db-password-from-2021
regex:AKIA[0-9A-Z]{16}
Source: GitHub Docs: Removing sensitive data from a repository
Where do secrets hide in Terraform repositories?
Terraform is a tool that builds cloud and network infrastructure from code. Its repositories hide secrets in three places. HashiCorp says that if you add secret values directly to your configuration, "Terraform stores those secrets in its state and plan files", and that when you work locally "Terraform stores your state in a plaintext file". Its advice is to treat the state file as sensitive and exclude it from Git (HashiCorp).
The third place is variable files. Terraform reads values from terraform.tfvars, terraform.tfvars.json and any *.auto.tfvars files (HashiCorp). Teams often put a database password or a provider token in one of them and commit it by mistake. Scan for *.tfstate, *.tfstate.backup, plan files and *.tfvars across the whole history, not only the current tree. The infrastructure as code examples page shows what the rest of such a repository holds.
| File | What it can hold | What to do |
|---|---|---|
| terraform.tfstate and backups | Every resource attribute, including initial passwords and tokens, in plain text | Keep it in a remote backend; remove it from history |
| Saved plan files | The same values as state, for one planned change | Never commit; remove from history |
| terraform.tfvars, *.auto.tfvars | Variable values, often credentials | Keep secrets out; use a secrets manager or environment variables |
| Provider blocks in .tf files | Hard-coded access keys | Rotate, then replace with --replace-text |
Worked example: cleaning a Terraform repository before it is shared
An illustration, not a real company. A hosting company keeps its network and cloud setup in one Terraform repository with six years of history. Before any copy leaves the building, the platform team runs the steps below on a fresh mirror clone.
| Step | What the team does | What it finds or changes |
|---|---|---|
| 1. Scan | gitleaks over all commits, then TruffleHog on the same clone | 14 findings; TruffleHog marks 2 as live |
| 2. Rotate | Rotate the two live keys and the old database password at their source | Old values stop working |
| 3. List | Write every leaked string into ../passwords.txt; list committed state and tfvars paths | 9 strings, 3 file paths (one renamed in 2022) |
| 4. Rewrite | git-filter-repo with --invert-paths for the files, then --replace-text for the strings | All commit hashes after 2020 change |
| 5. Re-scan | Run both scanners again on the rewritten clone | Zero findings |
| 6. Record | Keep a map of old to new commit hashes and the list of rotated secrets, without their values | Reviews and tickets can still be matched to commits |
Fictional data written for this page. The hash map in step 6 matters: tickets and incident notes often cite commit hashes, and the rewrite changes them.
Common mistakes when removing secrets from git
Most failed cleanups miss one of these.
- Rewriting history but never rotating the secret, so the old value still works for anyone with an old clone.
- Deleting the file in a new commit and calling it done.
- Scanning only the main branch. Old feature branches and tags keep the secret.
- Missing a renamed path, so one copy of the file survives the rewrite.
- Letting a teammate merge from a clone made before the rewrite, which brings the history back.
- Using git filter-branch, which the git manual no longer recommends.
- Leaving Terraform state, plan files or tfvars in history because the scanner did not flag them as a pattern.
- No pre-commit check afterwards. GitHub and OWASP both suggest scanning before each commit so the leak does not repeat.
Why is this the first job before config history leaves the company?
Because configuration history is only shareable once it is clean. AI data buyers have asked Origin for configuration and infrastructure-as-code history (Terraform, Ansible, firewall rule sets and routing) with its commits and reviews, going back years, and for incidents joined to the change that fixed them: the ticket, the chat thread, the config diff or pull request, and the outcome. That record comes from the company's own infrastructure and its own incidents, and the company must own it. The first step buyers asked for is small: two or three incidents end to end plus a slice of the config history. See the hub page for who holds these records and config drift for how changes tie to tickets.
Origin is an independent data partner. Your company decides what is shared and signs any license itself, and a buyer may pay Origin when a deal closes.
- Secrets first: rotate, scan every branch and tag, rewrite, re-scan. Keep the old-to-new hash map so reviews and tickets still line up.
- State, plan and tfvars files: remove them from the history that is shared.
- Customer and client data: resource names, tenant IDs and IP addresses can identify customers. Client infrastructure stays out unless the client has agreed.
- Personal data: author emails and names in commits and reviews usually need pseudonyms. See security incident data for the redaction table.
- Third-party code and terms: check the licenses of vendored modules and any contract that limits use of the records. See source code.
Does your company hold records like these? See what your data is worth.
Common questions
Does deleting a file in git remove it from history?
No. Deleting a file and committing removes it from the newest commit only. Every earlier commit still contains the file, and anyone with a clone can read it. To remove it from history you must rewrite the history with a tool such as git filter-repo and force-push.
Should I rotate a leaked secret before rewriting git history?
Yes. Rotate or revoke it first. Rewriting history cannot reach copies in other clones, forks or cached views, so the old value must stop working. GitHub says rotation alone may be enough in some cases, but OWASP still advises removing the exposed secret from code and logs.
What is the difference between gitleaks and TruffleHog?
Both scan git history for secrets. gitleaks matches pattern rules over every patch and is often run as a pre-commit hook. TruffleHog classifies over 800 secret types and can log in to check whether a found secret still works, which separates live leaks from dead ones.
Is git filter-repo better than git filter-branch?
Yes, for removing secrets. The git manual says filter-branch is not recommended because of safety and performance problems and points to git filter-repo. GitHub's own procedure uses git filter-repo 2.47 or later with the --sensitive-data-removal flag, which also reports the first changed commit and helps clean up other clones.
How do I remove a password from all commits but keep the file?
Use git filter-repo with --replace-text and a file that lists the password, one string per line. Every match in every commit is replaced, by default with ***REMOVED***, while the rest of the file stays. Rotate the password first and re-scan the rewritten clone afterwards.
Should Terraform state files be committed to git?
No. HashiCorp says local state is a plaintext file that includes any secret values in your configuration, and advises keeping it out of Git workflows. Use a remote backend for state, and keep plan files and tfvars files with credentials out of the repository too.
What happens to pull requests after a git history rewrite?
Their diffs can break. GitHub says the rewrite changes commit hashes from the first affected commit onward, closed pull request diffs may no longer display, and comments on open ones can be lost. Merge or close open pull requests before you rewrite.
Sources and further reading
- GitHub Docs: Removing sensitive data from a repository
- gitleaks README (GitHub)
- TruffleHog README (GitHub)
- git filter-repo README (GitHub)
- git filter-repo manual
- Git manual: git-filter-branch
- HashiCorp: Manage sensitive data in your Terraform configuration
- HashiCorp: Terraform input variables
- OWASP Secrets Management Cheat Sheet
