Nexus Void Research

Secrets, Git, git filter-repo, BFG Repo-Cleaner, Secret Scanning, CWE-798, Credential Rotation, How-to

How to Remove Secrets From Git History (Step by Step, 2026)

How to remove secrets from git history: rotate the key, rewrite history with git filter-repo or BFG, force-push, and stop the next leak. Exact commands.

To remove secrets from git history, first revoke and rotate the secret, because anyone who cloned the repository already has it. Then rewrite history with git filter-repo (or BFG Repo-Cleaner) to delete the file or replace the secret text in every commit, force-push all branches and tags, and have every collaborator re-clone. Finally, add scanning so the next secret is caught before it is committed.

A leaked API key, database password or private key in a commit is one of the most common ways code turns into a breach. MITRE tracks it as CWE-798: Use of Hard-coded Credentials, and the OWASP Secrets Management Cheat Sheet is blunt about the order of work: revoke first, clean up second. This guide walks through both, with the exact commands.

Why is deleting the file not enough?

Deleting a secret in a new commit only removes it from the latest version of the code. Git keeps every earlier commit, so git log -p or a checkout of an old commit still shows the secret. Anyone with a clone, a fork or a CI cache has a full copy of that history, and on a public repository anyone on the internet can read it.

That is why the steps below start with rotation. Rewriting history is cleanup. Rotation is the fix.

Step 1: Revoke and rotate the secret

Treat the secret as compromised the moment it was pushed, even to a private repository.

  1. Revoke the exposed credential in the provider's console (cloud, payment, email, database).

  2. Issue a new one and store it outside the code, in a secret manager or your CI platform's encrypted secrets.

  3. Deploy the new secret to every environment that used the old one.

  4. Check the provider's logs for use of the old credential between the leak and the revocation.

Only after the old secret stops working is it safe to spend time on history.

Step 2: Find every commit that contains the secret

Search the full history, on all branches, before you rewrite anything. A secret is often committed more than once, or copied into a second file.

Search for a known value or prefix across all commits:

git log -p -S 'AKIAIOSFODNN7EXAMPLE' --all
git log -p -S 'AKIAIOSFODNN7EXAMPLE' --all
git log -p -S 'AKIAIOSFODNN7EXAMPLE' --all

List every file path that ever existed, to spot files like .env or credentials.json:

git log --all --pretty=format: --name-only | sort -u
git log --all --pretty=format: --name-only | sort -u
git log --all --pretty=format: --name-only | sort -u

Or scan the whole history with an open-source scanner such as Gitleaks (gitleaks git -v in current versions, gitleaks detect -v in older ones). Write down every file and every secret value you find; you will need them in step 4.

Step 3: Back up and tell your team

Rewriting history changes every commit ID after the first bad one, so prepare before you run anything.

  • Make a full backup: git clone --mirror <repo-url> repo-backup.git

  • Tell collaborators to push or save their open work, and agree a time when nobody pushes.

  • Note open pull requests. Their branches will need to be rebased or recreated after the rewrite.

  • Temporarily allow force-pushes on protected branches, and remember to restore the protection afterwards.

Step 4: Rewrite history with git filter-repo

git filter-repo is the tool the Git project itself recommends over the older git filter-branch, which the Git documentation warns is slow and easy to get wrong. Install it with pip install git-filter-repo or brew install git-filter-repo, then work in a fresh clone (filter-repo refuses to run on a clone with local changes unless forced):

git clone <repo-url> repo-clean
cd repo-clean
git clone <repo-url> repo-clean
cd repo-clean
git clone <repo-url> repo-clean
cd repo-clean

Option A: remove a whole file from every commit. Use this when the secret lives in a file that should never have been committed, like .env:

git filter-repo --invert-paths --path .env --path config/credentials.json
git filter-repo --invert-paths --path .env --path config/credentials.json
git filter-repo --invert-paths --path .env --path config/credentials.json

Option B: replace the secret text but keep the file. Use this when the secret is one line inside a file you need to keep. Create a file called replacements.txt with one secret per line:

AKIAIOSFODNN7EXAMPLE==>REMOVED
sk_live_51Hxxxxxxxxxxxx==>REMOVED
regex:password\s*=\s*"[^"]+"==>password = "REMOVED

AKIAIOSFODNN7EXAMPLE==>REMOVED
sk_live_51Hxxxxxxxxxxxx==>REMOVED
regex:password\s*=\s*"[^"]+"==>password = "REMOVED

AKIAIOSFODNN7EXAMPLE==>REMOVED
sk_live_51Hxxxxxxxxxxxx==>REMOVED
regex:password\s*=\s*"[^"]+"==>password = "REMOVED

Then run:

git filter-repo --replace-text replacements.txt
git filter-repo --replace-text replacements.txt
git filter-repo --replace-text replacements.txt

Lines are treated as literal text unless they start with regex: or glob:. Without ==>, filter-repo replaces the match with ***REMOVED***. Delete replacements.txt when you are done, since it contains the secrets.

What about BFG Repo-Cleaner?

BFG Repo-Cleaner does the same job and is popular because its commands are short: java -jar bfg.jar --delete-files .env or java -jar bfg.jar --replace-text replacements.txt. By default it does not touch your latest commit, so delete the secret from the current code in a normal commit first. After BFG, clean up the old objects:

git reflog expire --expire=now --all && git gc --prune=now --aggressive
git reflog expire --expire=now --all && git gc --prune=now --aggressive
git reflog expire --expire=now --all && git gc --prune=now --aggressive


git filter-repo

BFG Repo-Cleaner

Recommended by the Git docs

Yes

No (third-party)

Needs

Python 3

Java

Remove whole files

--invert-paths --path

--delete-files

Replace secret text

--replace-text

--replace-text

Cleans the latest commit

Yes

No by default

Runs gc for you

Yes

No, run it yourself

Step 5: Verify the secret is gone

Never force-push before checking. Run the same searches from step 2 in the rewritten clone:

git log -p -S 'AKIAIOSFODNN7EXAMPLE' --all
gitleaks git -v
git log -p -S 'AKIAIOSFODNN7EXAMPLE' --all
gitleaks git -v
git log -p -S 'AKIAIOSFODNN7EXAMPLE' --all
gitleaks git -v

Both should come back empty for the values you removed. Check that the project still builds, because removing a whole file can break code that imported it.

Step 6: Force-push and clean up every copy

filter-repo removes the origin remote as a safety step, so add it back and push everything:

git remote add origin <repo-url>
git push origin --force --all
git push origin --force --tags
git remote add origin <repo-url>
git push origin --force --all
git push origin --force --tags
git remote add origin <repo-url>
git push origin --force --all
git push origin --force --tags

Then deal with the copies you do not control:

  • Collaborators must re-clone. Merging an old clone back in will reintroduce the secret.

  • Forks and mirrors keep the old history. Ask their owners to delete and re-fork.

  • Your Git host may still serve old commits through cached views, pull request pages or direct commit links. Check your host's documentation for how to request removal of cached data.

  • CI caches and artifacts may contain the old repository. Clear them.

  • Restore branch protection you relaxed in step 3.

This is why step 1 matters most: you can never be fully sure every copy is gone, but a revoked secret is useless.

Step 7: Stop the next secret from being committed

Most teams who clean history once end up doing it again. Prevention is cheaper:

  • Ignore secret files by adding .env, *.pem and similar to .gitignore, and commit a .env.example with placeholder values instead.

  • Use a secret manager or your CI platform's encrypted secrets, and read secrets from environment variables at runtime.

  • Scan before commit with a pre-commit hook that runs a secret scanner on staged changes.

  • Scan every pull request so a secret that slips past a local hook is blocked before merge.

  • Scan the default branch on a schedule, because secrets also arrive through merges, vendored code and generated files.

We cover detection in more depth in how to find hardcoded secrets in code, and pipeline hardening in how to secure a CI/CD pipeline with GitHub Actions. If you are cleaning up after an AI coding assistant, see how to secure AI-generated code, since generated code often hardcodes example keys.

How does ARGUS help with leaked secrets?

ARGUS finds the secrets in your code before they become a history-rewrite problem, and tells you which ones are dangerous right now.

  1. It scans for secrets in three passes: vendor-specific patterns (Gitleaks), entropy analysis for random-looking strings, and named-assignment checks that catch a literal assigned to a variable like api_key or client_secret.

  2. It filters the noise that makes most scanners ignorable: test and fixture paths, documentation placeholders such as AWS example keys, publishable keys, environment variable references, git SHAs and lockfiles.

  3. It tells you which secrets are live. With AI scanning on, each finding is marked Live, Rotated, Test or Unknown, so you rotate the live keys first instead of triaging a list of fixtures.

  4. It reviews every pull request and can post a required status check, so a newly added secret can be blocked before it reaches the default branch and ends up in history.

Limits: ARGUS reports up to 60 secret findings per pass and skips files larger than 1 MB. Secret scanning is part of every ARGUS scan; see ARGUS secret scanning for details, or scan a repository free.

Frequently asked questions

How do I remove a secret from git history?

Revoke and rotate the secret first. Then, in a fresh clone, run git filter-repo with --invert-paths --path <file> to delete the file from every commit, or --replace-text to replace the secret string. Verify with git log -S and a scanner, force-push all branches and tags, and have collaborators re-clone.

Does deleting a file in a new commit remove it from git history?

No. A new commit only removes the file from the latest version. Every earlier commit still contains it, and anyone with a clone, fork or CI cache can read it with git log -p or by checking out an old commit. You must rotate the secret and rewrite history to remove it.

Should I rotate a secret if the repository is private?

Yes. Private repositories are shared with collaborators, CI systems, integrations and sometimes contractors, and access changes over time. Treat any secret that was committed as exposed: revoke it, issue a new one, check the provider's logs for misuse, then clean up the history.

Is git filter-repo better than BFG Repo-Cleaner?

Both work. git filter-repo is recommended by the Git project, cleans the latest commit too and runs garbage collection for you, but needs Python. BFG needs Java, has shorter commands and leaves your latest commit untouched by default, so you must remove the secret from current code first and run git gc afterwards.

Why should I not use git filter-branch?

The Git documentation itself warns that git filter-branch is slow on large repositories and has pitfalls that can silently produce a broken or incomplete rewrite. git filter-repo was built to replace it for this kind of job, and is faster and safer for removing files or text from every commit.

What happens to forks and pull requests after rewriting history?

Forks keep the old history, so ask their owners to delete and re-fork. Open pull requests point at old commits and need to be rebased or recreated. Your Git host may also keep cached views of old commits, so check its documentation for how to request removal.

How do I remove a .env file from git history?

Rotate every secret in the file first. Then run git filter-repo --invert-paths --path .env in a fresh clone, verify it is gone, and force-push all branches and tags. Add .env to .gitignore and commit a .env.example with placeholder values so it is not committed again.

How do I find secrets in my git history?

Search all branches with git log -p -S '<value>' --all for a known secret, or scan the whole history with an open-source scanner such as Gitleaks using gitleaks git -v. List every path that ever existed with git log --all --name-only to spot files like .env or key files.

How can I prevent secrets from being committed?

Keep secrets in a secret manager or encrypted CI secrets and read them from environment variables. Add secret files to .gitignore, run a secret scanner in a pre-commit hook, and scan every pull request so a secret that slips past the hook is blocked before it merges.

DATA SOURCES

git filter-repo: https://github.com/newren/git-filter-repo ; Git docs git-filter-branch: https://git-scm.com/docs/git-filter-branch ; OWASP Secrets Management Cheat Sheet ; CWE-798

Liked this post? Share it: