This guide shows how to permanently delete a file from all commits in a Git repository, including its entire history.
- Git installed
- Python 3.x installed (for
git-filter-repo) - Administrator/owner access to the repository
# Check if already installed
git filter-repo --version || pip install git-filter-repogit clone --mirror https://github.com/yourusername/yourrepo.git
cd yourrepo.gitReplace sensitive-file.txt with your actual filename/path:
git filter-repo --path sensitive-file.txt --invert-pathsgit push origin --force --all
git push origin --force --tagscd ..
rm -rf yourrepo.gitSituation: You accidentally committed config.ini with database credentials and need to remove it from all history.
Solution:
git clone --mirror https://github.com/yourcompany/webserver.git
cd webserver.git
git filter-repo --path config.ini --invert-paths
git push origin --force --all
git push origin --force --tags
cd ..
rm -rf webserver.git
For very large repositories, consider:
- BFG Repo-Cleaner
git filter-branch(older method)