| name | git-rewrite-history |
| description | MANDATORY: Use instead of `git filter-branch` - safer git-filter-repo with recovery |
Git Rewrite History Skill
Purpose: Safely rewrite git history using git-filter-repo, the modern replacement for
git filter-branch.
Why git-filter-repo over git-filter-branch
Always use git-filter-repo instead of git filter-branch. Git itself warns against filter-branch:
git-filter-branch has a glut of gotchas generating mangled history rewrites.
Use git-filter-repo instead.
| Feature | git-filter-repo | git-filter-branch |
|---|
| Speed | 10-50x faster | Slow |
| Safety | Handles edge cases | Many gotchas |
| Maintenance | Actively maintained | Deprecated |
| Syntax | Simple, intuitive | Complex, error-prone |
Installation
pip install git-filter-repo
pip install --break-system-packages git-filter-repo
Safety Pattern: Backup-Verify-Cleanup
ALWAYS follow this pattern:
- Work on a fresh clone (filter-repo requires this by default)
- Create backup of original remote URL
- Execute the filter operation with
--partial (preserves reflog for recovery)
- Verify immediately - check history is correct
- Force push only after verification and explicit user approval
MANDATORY: Always Use --partial Flag
Always include the --partial flag with git-filter-repo. Without it:
- Reflog is expired (old commits unrecoverable)
- Automatic
git gc runs (objects permanently deleted)
- No recovery possible after force-push
With --partial:
- Old commits preserved in reflog
- Recovery via
git reset --hard HEAD@{n} possible
- New history is active, old history available as fallback
Common Operations
Remove a File from All History
git clone --mirror <url> repo-filter
cd repo-filter
git filter-repo --partial --path secrets.txt --invert-paths
git log --all --oneline -- secrets.txt
git push origin --force --all
Remove a Directory from All History
git filter-repo --partial --path vendor/ --invert-paths
Remove a Submodule from History
git filter-repo --partial --path submodule-name --invert-paths
Rename/Move Files in History
git filter-repo --partial --path-rename old-name.txt:new-name.txt
git filter-repo --partial --path-rename old-dir/:new-dir/
Remove Large Files
git filter-repo --partial --strip-blobs-bigger-than 10M
Keep Only Specific Paths
git filter-repo --partial --path src/
Filter by Content (Remove Secrets)
git filter-repo --partial --replace-text expressions.txt
Working on Existing Clone
By default, git-filter-repo requires a fresh clone. To work on an existing repo:
git filter-repo --partial --force --path file-to-remove --invert-paths
After Rewriting History
- All collaborators must re-clone or reset their branches
- Force push required:
git push --force-with-lease origin <branch>
- Update any CI/CD that caches the old commits
- GitHub/GitLab: May need to run garbage collection on server
Verification Checklist
Recovery
If something goes wrong (before force-push):
git reflog
git reset --hard HEAD@{2}
git fetch origin
git reset --hard origin/<branch>
git reset --hard backup-before-filter
CRITICAL: Recovery from reflog only works if:
- You used
--partial flag
- You haven't force-pushed yet (or haven't run
git gc)
When to Use This Skill
- Removing accidentally committed secrets
- Removing large binary files to reduce repo size
- Removing submodules from history
- Restructuring repository paths
- Splitting a repository
References