Remove folder and its contents from Git/GitHub's history
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Deleting a folder in the latest commit does not remove it from Git history. If the folder contained secrets, large generated files, or anything else that should never have been committed, you need to rewrite history so that path disappears from every commit where it existed.
Understand the Cost First
History rewriting changes commit IDs. That means:
- commit hashes change
- collaborators must resync carefully
- open pull requests may become awkward or invalid
- old clones may still contain the data until they clean up
So the real workflow is not just one command. It is rewrite, verify, force-push, coordinate, and rotate secrets if sensitive data was exposed.
Use git filter-repo
The modern recommended tool for this job is git filter-repo. It is faster and easier to reason about than older filter-branch workflows.
Start with a mirror clone:
Then remove the folder from all history:
--invert-paths means "keep everything except this path."
If you need to remove multiple folders:
This rewrites every affected commit in the mirrored repository.
Verify the Rewrite Before Pushing
Do not force-push until you confirm the path is really gone.
If no commits appear, the path is gone from reachable history in the rewritten repository.
If the cleanup was motivated by large files, you should also inspect repository size and the object graph before pushing the rewritten refs back to the remote.
Force-Push Carefully
Once you verify the rewrite, push the new history:
This is the step that updates the remote for everyone. On a shared repository, it should be coordinated explicitly because every collaborator's local history becomes incompatible with the remote.
Rotate Secrets If Needed
If the removed folder contained credentials, rewriting history is not enough. Assume the data was already exposed and rotate it.
Typical examples:
- API keys
- cloud credentials
- certificates
- database passwords
History cleanup reduces future exposure, but it does not make previously leaked secrets safe again.
Tell Collaborators What to Do
After a destructive history rewrite, other developers should not continue from old clones casually. The safest advice is usually to re-clone.
If re-cloning is not practical, they must hard-reset to the rewritten remote and clean unreachable objects, which is easier to get wrong than starting fresh.
A useful team message usually includes:
- why the rewrite happened
- when the force-push occurred
- whether everyone should re-clone
- any secret-rotation steps
That communication is part of the technical fix, not an optional extra.
Prevent the Folder From Returning
Once the history is clean, add ignore rules so the same folder does not come back later.
This does not remove history by itself, but it prevents the exact same mistake from being recommitted.
Common Pitfalls
The biggest mistake is deleting the folder in a new commit and assuming that removes it from old history. It does not.
Another common issue is rewriting history and force-pushing without warning collaborators, which leaves everyone else with confusing local state.
People also forget to rotate secrets. If credentials were ever committed, treat them as compromised whether or not the history is cleaned later.
Finally, do not skip verification. A cleanup command that runs successfully is not proof that the unwanted path actually disappeared from all reachable history.
Summary
- Removing a folder from Git history requires history rewriting, not a normal delete commit.
- '
git filter-repo --path folder/ --invert-pathsis the preferred modern approach.' - Verify the rewritten history before force-pushing it to the remote.
- Coordinate with collaborators because commit hashes will change.
- Rotate any exposed secrets and add
.gitignorerules to prevent recurrence.
Related reading
- Remove large .pack file created by Git
- Remove last commit from remote Git repository
- Remove local git tags that are no longer on the remote repository
- Remove refs/original/heads/master from git repo after filter-branch --tree-filter?
- Remove sensitive files and their commits from Git history
- Remove specific commit
- Remove tracking branches no longer on remote
- Removing multiple files from a Git repo that have already been deleted from disk
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.