Skip to content

Instantly share code, notes, and snippets.

@sandeepkv93
Created July 12, 2025 01:26
Show Gist options
  • Select an option

  • Save sandeepkv93/a449fc48a78073d5ba985052d1956ef6 to your computer and use it in GitHub Desktop.

Select an option

Save sandeepkv93/a449fc48a78073d5ba985052d1956ef6 to your computer and use it in GitHub Desktop.
Remove File from All Git Commits

Complete Guide: Remove File from All Git Commits

This guide shows how to permanently delete a file from all commits in a Git repository, including its entire history.

Prerequisites

  • Git installed
  • Python 3.x installed (for git-filter-repo)
  • Administrator/owner access to the repository

Step-by-Step Instructions

1. Install git-filter-repo

# Check if already installed
git filter-repo --version || pip install git-filter-repo

2. Create a fresh clone of your repository

git clone --mirror https://github.com/yourusername/yourrepo.git
cd yourrepo.git

3. Remove the file from all commits

Replace sensitive-file.txt with your actual filename/path:

git filter-repo --path sensitive-file.txt --invert-paths

4. Push the changes to GitHub

git push origin --force --all
git push origin --force --tags

5. Clean up local repository

cd ..
rm -rf yourrepo.git

Example Scenario

Situation: 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

Important Warnings

⚠️ This rewrites history - All commit hashes will change
⚠️ Collaborators must reclone - Anyone with existing clones will need to get fresh copies
⚠️ Backup first - Consider creating a backup branch before proceeding
⚠️ For sensitive data - If removing passwords/keys, rotate them even after removal

Alternative Tools

For very large repositories, consider:

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment