Remove a Large or Secret File From Git History
Deleting a committed file only hides it from the latest commit. The blob still lives in history, and if it was a secret, it's already compromised.
Key takeaways
- Deleting a file with git rm leaves the blob in every earlier commit, so you must rewrite history.
- Rotate any leaked secret first, then run git filter-repo --invert-paths --path <file> on a fresh clone and force-push.
- Every collaborator must re-clone, and GitHub Support has to purge cached views and pull request refs.
On this page
To remove a file from Git history, rewrite every commit that contained it: run git filter-repo --invert-paths --path <file> on a fresh clone, then force-push all branches and tags. git rm alone only drops the file from the latest commit. If the file held a secret, rotate the secret first, because scrubbing never un-leaks it.
Verified against
git-filter-repo 2.47.0, BFG Repo-Cleaner 1.15.0, Git LFS 3.8.1 and gitleaks 8.30.1, against the GitHub Docs sensitive-data and large-file pages and the git-filter-branch man page. Checked October 2026.
Someone commits a 400MB training dataset. Or an .env with a live AWS key. They notice, run git rm, commit, push, and figure it's handled. It isn't. Every previous commit still points at the blob, and the blob still sits in the pack files. GitHub still rejects the push, because it blocks any file larger than 100 MiB (and warns above 50 MiB). The secret is still readable by anyone who clones and runs git log -p.
History in Git is content-addressed. You don't edit it, you rewrite it: build a new chain of commits that never contained the file, then throw the old chain away. The tooling matters.
Rotate the secret first, then scrub
If what leaked was a credential, stop reading and go invalidate it. Rotate the key, revoke the token, change the password. Do it before you touch the history.
The reasoning people skip: the moment a secret hits a push, assume it's captured. Automated scanners watch public pushes. On GitHub the commit stays reachable through cached views and pull request refs, and GitHub's own docs warn that any fork containing the commit keeps it accessible after your rewrite. Even a private repo has CI logs, clones on laptops, and backups. Scrubbing history makes the secret hard to find again. It does not make it safe. The only thing that makes a leaked credential safe is that it no longer works.
So the order is fixed: rotate, confirm the old value is dead, then clean the repo so you're not shipping a landmine to the next person who clones.
Use git filter-repo, not filter-branch
The old advice was git filter-branch. Don't. It's slow, it's easy to get wrong, and its own man page says its "use is not recommended" and points to git filter-repo instead. On a repo of any size it crawls, and a subtle mistake leaves the blob behind while looking like it worked.
The modern tool is git filter-repo. It needs Git 2.36 or later and Python 3.6 or later; install it with pip install git-filter-repo or brew install git-filter-repo. GitHub's guide asks for at least 2.47, the release that added --sensitive-data-removal, which also reports the first changed commits you need if you contact GitHub Support.
Work on a fresh clone. filter-repo is aggressive by design and removes your remotes as a safety measure, so don't run it on your only copy.
$ git clone https://github.com/acme/api.git
$ cd api
To strip a specific file everywhere it ever existed:
$ git filter-repo --sensitive-data-removal --invert-paths --path config/secrets.yml
--path names the file, --invert-paths flips the meaning to "keep everything except this." You can pass --path multiple times, or use --path-glob '*.pem' to catch a pattern. Use the full path from the repo root, and add every earlier path if the file was ever renamed. For a password pasted into a tracked file, --replace-text swaps the string across history instead of deleting the file.
For a bloated repo where you don't care which file, only that it's huge:
$ git filter-repo --strip-blobs-bigger-than 50M
That rewrites every commit, dropping any blob over 50MB. Good for cleaning up a history where someone kept committing build artifacts or model checkpoints.
Once it finishes, re-add your remote and force-push:
$ git remote add origin https://github.com/acme/api.git
$ git push origin --force --all
$ git push origin --force --tags
BFG when you want simple
BFG Repo-Cleaner (a Java tool, 1.15.0 as of October 2026) does less than filter-repo but does it fast, and the syntax is friendlier if you're doing one of the two things it's built for: deleting files by name or replacing secret strings.
$ git clone --mirror https://github.com/acme/api.git
$ java -jar bfg.jar --delete-files id_rsa api.git
$ java -jar bfg.jar --strip-blobs-bigger-than 50M api.git
The --mirror clone gives BFG the bare repo it wants. After it runs, garbage-collect and push:
$ cd api.git
$ git reflog expire --expire=now --all
$ git gc --prune=now --aggressive
$ git push --force
BFG protects your latest commit by default, on the theory that HEAD is your current good state. That's usually what you want, but if the bad file is still in HEAD, remove it with a normal commit first.
The part everyone underestimates: it breaks every clone
Rewriting history changes every commit hash from the rewrite point forward. Your force-push replaces the remote's history, but nobody else's local copy knows that. If a teammate pulls the old way, Git happily merges the deleted file right back in, and you're back where you started.
There's no clean fix, only coordination. Before you push, tell everyone what's coming. After you push, everyone re-clones fresh, or at minimum resets their branches hard to the new remote. Any open pull request built on old commits has to be recreated, and on GitHub the old commits stay visible through cached views and PR refs until GitHub Support purges them. On a shared repo this is a scheduled event, not a Tuesday-afternoon whim. Treat it that way and it's fine. Surprise people and you'll spend a week chasing the file as it reappears from stale branches.
Stop it from happening again
Cleaning history is expensive enough that you only want to do it once. Three cheap guards keep the same mistake from landing twice.
Large files belong in Git LFS. Track the file types that keep bloating the repo and Git stores a pointer while the bytes go to LFS storage:
$ git lfs install
$ git lfs track "*.psd" "*.zip" "*.model"
$ git add .gitattributes
Obvious things belong in .gitignore before anyone can stage them: .env, *.pem, credentials.json, node_modules/, build output directories.
For secrets specifically, run a scanner as a pre-commit hook so the leak never reaches a push. Gitleaks is the common pick:
# .pre-commit-config.yaml
repos:
- repo: https://github.com/gitleaks/gitleaks
rev: v8.30.1
hooks:
- id: gitleaks
Then pre-commit install, and a commit carrying an AWS key or a private key gets blocked locally before it ever hits the remote. It won't catch everything, but it catches the careless paste, which is how most leaks start. Our secret scanning guide covers server-side scanning for what slips past the hook.
If you want more on untangling Git states before they escalate, our Git troubleshooting guide covers the everyday cases.
Questions people ask
Does git rm remove a file from history?
No. git rm deletes the file from the next commit only. Every earlier commit still references the blob, so git log -p or a checkout of an old commit brings it back, and GitHub still blocks the push if the file exceeds 100 MiB. Removing it from history means rewriting commits with git filter-repo --invert-paths --path <file> and force-pushing.
Is a leaked secret safe after I rewrite history?
No. Rewriting makes the secret harder to find, not harmless. Clones, forks, CI logs and backups may already hold it, and GitHub's docs say forks containing the commit stay accessible. Revoke or rotate the credential first, confirm the old value fails, and only then scrub history. GitHub Support can purge cached views and pull request refs afterwards.
Should I use git filter-repo or BFG?
Use git filter-repo by default. Git's own filter-branch man page recommends it, GitHub's sensitive-data guide is written around it, and it handles paths, globs, size limits and text replacement. BFG is fine for its two jobs, deleting files by name and stripping big blobs, but it needs Java and protects HEAD, so commit the deletion first.
The call we'd make
If it's a secret, rotate it right now, this minute, and consider the value burned no matter what you do next. Then clean the history with git filter-repo unless your case is a simple file-delete, in which case BFG is faster to reach for. Force-push, then get everyone on a fresh clone the same day so the blob doesn't crawl back. Finally, wire up LFS, .gitignore, and gitleaks. The scrub is the emergency; the guards are how you avoid the next one.
Sources
Cite this article
Kiril Urbonas (2026). Remove a Large or Secret File From Git History. DevOpsNess. Updated 2026-10-31. https://www.devopsness.com/blog/remove-large-file-git-history
[Remove a Large or Secret File From Git History](https://www.devopsness.com/blog/remove-large-file-git-history) (Kiril Urbonas, DevOpsNess, updated 2026-10-31)
More from DevOps
Explore more articles in this category
kaniko CVE-2026-105769: Lookalike ACR Hosts Get Your Azure Token. Rebuild on v1.25.20
A FROM line pointing at a host like x.azurecr.io.attacker.com is enough to make kaniko hand the build's Entra ID token to that host. Which kaniko forks and images are affected, how to find Azure identities in your build pods, and what to rotate.
Sign Container Images With Cosign: Keyless GitHub Actions Workflow, SBOM and In-Cluster Checks
One workflow that builds, signs keylessly with GitHub OIDC, attaches an SBOM and SLSA provenance, plus the Kyverno policy that refuses anything unsigned. Copy it, change three names, ship it.
GitLab CVE-2026-110264: A Second CVSS 10 RCE in Nine Days. Patch to 19.5.1
An unauthenticated regex-compiler RCE reaching back to GitLab 7.0, no workaround, and backports on older branches that fix only the critical bug. The exact version per branch and the commands to get there.
You might have missed
Evergreen posts worth revisiting.