Removing secrets from history
A committed secret stays in history after you delete the file. Rotate it first, then rewrite history, force push and clean up, with a real filter-branch run.
The reflog and recovering lost workForce pushing safelyIgnoring files with .gitignore
Removing secrets from history
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Deleting a file in a new commit doesn't remove it from history: anyone with a clone, and the remote, still has the old commits and the secret.
- Rotate or revoke the leaked secret first, because rewriting history can't un-leak something that was already public.
- `git filter-repo` is the recommended tool to rewrite history, and the older `git filter-branch` works but warns that it is deprecated.
- After a rewrite you must force push every affected branch and tag, then expire the reflog and run `git gc --prune=now` to drop the old objects locally.
- Everyone else with a clone must re-clone or reset onto the new history, and the hosting service may still cache old commits.
Delete is not remove
You are adding a sync feature to jot and commit a .env file with an API token. Two commits later, you notice. You run git rm --cached .env, add .env to .gitignore and commit. The file is gone from the project. Is it gone from the repository?
$ ls -A
.env .gitignore README.md config.yml
$ git log --oneline --all -S"fake_live"
a571bb7 Ignore .env
043c836 Add sync config
$ git show HEAD~2:.env
SYNC_TOKEN=fake_live_9f8e7d6c5b4a
The token is one git show away. Commits are immutable snapshots, and “Add sync config” still contains .env. The “Ignore .env” commit only records that the file stopped being tracked. (The token here is a made-up string.)
Step one: assume it is public
Before touching Git, revoke or rotate the secret. If the repository was ever pushed to a place others could read, treat the secret as copied: bots scan new commits within minutes. Removing it from history afterwards limits future exposure. It does not undo the leak. Do this first, every time.
Step two: rewrite the history
The recommended tool is git filter-repo, a separate program. It wasn’t installed on the machine where I wrote this lesson, so I could not run it. From its documentation, the equivalent of what I did next is:
git filter-repo --invert-paths --path .env
which drops that path from every commit. For secrets inside files you want to keep, --replace-text rewrites matching strings. Treat these two commands as unverified from my side.
What I did run is git filter-branch, which ships with Git, is deprecated, slower, and easy to misuse:
$ git filter-branch --index-filter 'git rm --cached --ignore-unmatch -q .env' -- --all
WARNING: git-filter-branch has a glut of gotchas generating mangled history
rewrites. Hit Ctrl-C before proceeding to abort, then use an
alternative filtering tool such as 'git filter-repo'
...
Rewrite a571bb7... (4/4)
Ref 'refs/heads/main' was rewritten
Ref 'refs/heads/release' was rewritten
Ref 'refs/remotes/origin/main' was rewritten
Ref 'refs/remotes/origin/release' was rewritten
--index-filter runs the command against each commit’s index without checking files out, --ignore-unmatch keeps it quiet for commits that never had .env, and -- --all rewrites every branch. Every commit from the first one that contained .env onward got a new hash: 043c836 became 6204646, and so on. (Hashes will differ for you.)
Step three: it is still there
Straight after, the secret is still findable:
$ git log --all --oneline -S"fake_live"
a571bb7 Ignore .env
043c836 Add sync config
$ git for-each-ref
...
a571bb7... commit refs/original/refs/heads/main
...
filter-branch keeps backups of the old branch tips in refs/original/. Delete them, expire the reflog, and garbage collect:
$ git for-each-ref --format='%(refname)' refs/original/ | xargs -n1 git update-ref -d
$ git reflog expire --expire=now --all
$ git gc --prune=now
$ git log --all --oneline -S"fake_live" | wc -l
0
$ git cat-file -t 043c836
fatal: Not a valid object name 043c836
The old objects are really gone locally. (filter-repo does this cleanup itself.)
Step four: publish, carefully
Here my tidy command failed:
$ git push --force-with-lease --all
! [rejected] main -> main (stale info)
! [rejected] release -> release (stale info)
filter-branch --all had rewritten my origin/main bookmarks too, so the lease no longer matched what was on the remote, as explained in force pushing safely. Since I was deliberately replacing everything, a plain force worked:
$ git push --force --all origin
+ a571bb7...973b0bc main -> main (forced update)
+ 48e66f5...53134f0 release -> release (forced update)
Tags need the same treatment with --tags. The remote still held the old objects until its own garbage collection:
$ git --git-dir=../hub.git cat-file -t 043c836
commit
$ git --git-dir=../hub.git gc --prune=now
$ git --git-dir=../hub.git cat-file -t 043c836
fatal: Not a valid object name 043c836
On a self-run bare repository you can do that. On a hosting service you can’t run gc yourself. They can retain commits that were reachable through pull requests or cached views, and removing them means asking their support. I couldn’t test a hosting service, so that part is from general knowledge, not my run.
Everyone else
A teammate with an old clone fetches and sees both histories as diverged. In my run git pull --rebase in that clone recognised the old commits and ended on the new history, but the old objects remained in its reflog. Others must delete their old clones, or git fetch then git reset --hard origin/main, and must never merge the old branches back, which would reintroduce the secret.
Prevention beats cleanup
Put .env in .gitignore before the first commit, and consider a pre-commit hook or your host’s secret scanning to block pushes that contain tokens.
What the video covers
The Modern SDE’s “Remove a Secret from Git History: filter-branch and git filter-repo” is lesson 63 of a series, about eleven minutes, from late 2026. From its title it covers both tools, which fits this lesson’s coverage. I judge English from the title, description and channel only, and I could not read a transcript. IRB0T’s shorter filter-repo walkthrough is linked as a runner-up, since filter-repo is the tool I couldn’t run.
Where this fits
That ends rewriting and recovering. Next group: how Git really stores all this, starting with objects, blobs, trees and commits.
Check yourself
You committed `.env`, then deleted it and added it to `.gitignore`. Is the secret safe?
No. The earlier commit still contains the file, so anyone who can read the history can read the secret.
What is the first thing you do after a secret has been pushed?
Revoke or rotate it. Assume it was copied. Rewriting history is cleanup, not a substitute.
After `git filter-branch`, why does `git log -S` still find the secret before you clean up?
`filter-branch` keeps backups of the old branches under `refs/original/`, and the old objects also stay reachable through the reflog until you remove them.
Why did `git push --force-with-lease --all` fail after the rewrite in this lesson?
`filter-branch --all` rewrote the remote-tracking branches too, so the lease no longer matched the remote's real tips. A plain `--force` push was needed, or a fetch first.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.