git-filter-repo Guide: Safely Rewrite Git History and Remove Large Files

Git tutorial - IT technology blog
Git tutorial - IT technology blog

Quick start: Rescue your repository in 5 minutes

It’s 2:00 AM. Your CI/CD pipeline suddenly fails with a disk space error, or GitHub outright rejects your push:

remote: error: File data.dump is 180.50 MB; this exceeds GitHub's file size limit of 100.00 MB

The culprit is usually someone accidentally committing a database backup or an ML model file of several hundred megabytes to the main branch. Even if you run git rm in a subsequent commit, that file remains preserved in Git history. The repository stays bloated, and pushes keep failing.

That’s where git-filter-repo comes in. This is the official tool recommended by the Git documentation to completely replace the legacy git filter-branch.

Step 1: Install git-filter-repo

# On Ubuntu / Debian
sudo apt update && sudo apt install -y git-filter-repo

# Or via pip (requires Python 3.6+)
python3 -m pip install --user git-filter-repo

# On macOS via Homebrew
brew install git-filter-repo

Step 2: Create a backup (Mandatory)

Never rewrite Git history in your active working directory. Always create a fresh clone in a temporary folder:

cd /tmp
# Clone a pristine backup mirror
git clone --mirror https://github.com/your-org/your-repo.git repo-backup.git

# Clone a second copy to perform the cleanup
git clone https://github.com/your-org/your-repo.git repo-cleanup
cd repo-cleanup

Step 3: Completely remove large files from all branches and tags

It only takes a single command to purge unwanted files across the entire commit history:

git-filter-repo --path data.dump --invert-paths

Done! For a repo with roughly 10,000 commits, this command takes only about 5 to 15 seconds to run. The data.dump file disappears entirely, and your .git directory shrinks immediately.


Why ditch git filter-branch?

Older tutorials often mention git filter-branch or BFG Repo-Cleaner. However, the official Git documentation has marked git filter-branch as deprecated due to three major drawbacks:

  • Extremely slow: On a project with roughly 50,000 commits, filter-branch can take 2 to 3 hours because it checks out each commit to disk using subshells. In contrast, git-filter-repo leverages in-memory git fast-export and git fast-import, completing the same task in under 30 seconds (50x–100x faster).
  • Prone to metadata corruption: filter-branch frequently discards GPG signatures, corrupts timestamps, or incorrectly rewrites merge topology.
  • Fragile shell syntax: A single misplaced quote in a shell filter script can accidentally wipe out your entire codebase.

Another big plus: after running, git-filter-repo automatically unlinks the origin remote. This safeguard prevents you from accidentally running git push before thoroughly reviewing the rewritten history.


Three common real-world scenarios

1. Filter all files exceeding a specific size

You might not always know exactly which large files were committed or who added them. Let the tool automatically scan for any blob exceeding a specified threshold (e.g., larger than 10MB):

git-filter-repo --strip-blobs-bigger-than 10M

All files in git history 10MB or larger are cleanly stripped away without requiring you to hunt down individual commit hashes.

2. Replace accidentally leaked API keys or passwords

Accidentally committed a Stripe key or AWS access key? Create a replace.txt file containing the key-value replacement pairs:

# Syntax: <old_string>==><new_string>
sk_live_51Mxyz123456789==>REMOVED_SECRET_KEY
password_production_root==>DUMMY_PASSWORD

Then scan and rewrite the content across your entire history:

git-filter-repo --replace-text replace.txt

Every past commit containing the sensitive string will have it swapped for the dummy value, preserving your commit structure without introducing syntax errors.

3. Split a subfolder into a standalone repository

When you need to extract the services/payment directory into an independent microservice while retaining its full commit history:

git-filter-repo --subdirectory-filter services/payment

Instantly, the contents of services/payment become the repository root, and all commits modifying files outside this folder are purged.


Verification and team synchronization rules

Rewriting Git history alters the SHA commit hashes for every rewritten commit. Your entire team will encounter out-of-sync branches unless the rollout is carefully coordinated.

  • Verify repo size and commit logs:
    git log --oneline -n 10
    git count-objects -vH

    For example, if size-pack drops from 1.8 GiB to 45 MiB, the bloated files have been successfully cleaned up.

  • Reconfigure the remote: Once everything looks right, re-add your remote URL:
    git remote add origin [email protected]:your-org/your-repo.git
  • Use –force-with-lease instead of –force:
    git push origin --force-with-lease --all
    git push origin --force-with-lease --tags

    The --force-with-lease flag rejects the push if someone pushed new commits to the remote while you were cleaning, preventing accidental data loss of your colleagues’ work.

  • Handoff to your team: The safest route for team members is to delete their old local clones and clone fresh from the cleaned remote. Never run git pull on an outdated local clone, as doing so will merge the deleted bloated commits right back into the repo.
Share: