git_filter_branch
git filter-branch
Rewrite Git history (use with caution).
⚠️ Warning
History Rewriting
This rewrites history and changes commit hashes. Never use on shared branches.
🔧 Modern Alternative
Use git-filter-repo (Recommended)
pip install git-filter-repo
Install the modern alternative (faster, safer).
✉️ Change Author Info
Filter-repo: Change Email
git filter-repo --email-callback '
return email.replace(b"old@email.com", b"new@email.com")
'
Changes email throughout history.
Classic: Update Author
git filter-branch --env-filter '
if [ "$GIT_AUTHOR_EMAIL" = "old@email.com" ]; then
export GIT_AUTHOR_EMAIL="new@email.com"
fi
' -- --all
Legacy way to change author email.
🗑️ Remove Files
Filter-repo: Remove File
git filter-repo --path secrets.txt --invert-paths
Removes file from entire history.
Remove Folder
git filter-repo --path config/ --invert-paths
Removes entire folder from history.
BFG Cleaner (Alternative)
bfg --delete-files secrets.txt
Fast file removal tool.
BFG: Remove Large Files
bfg --strip-blobs-bigger-than 100M
Removes files larger than 100MB.
📁 Subdirectory to Root
Extract Subdirectory
git filter-repo --subdirectory-filter src/
Makes src/ the new root.
✏️ Rewrite Messages
Filter-repo: Replace Text
git filter-repo --message-callback '
return message.replace(b"old-text", b"new-text")
'
Replaces text in all commit messages.
🧹 After Rewriting
Force Push
git push --force --all
⚠️ Push rewritten history.
Push Tags
git push --force --tags
Push rewritten tags.
Clean Up
git reflog expire --expire=now --all
Expire old reflog entries.
git gc --prune=now
Remove old objects.
📊 Comparison
| Tool | Speed | Safety | Use Case |
|---|---|---|---|
| filter-repo | Fast | Good | Most cases |
| BFG | Fast | Good | Remove files/secrets |
| filter-branch | Slow | ⚠️ | Legacy |
💡 Tips
Backup First
git clone --mirror repo backup
Fresh Clone After
After rewriting, have team members clone fresh.
🔗 Related
#git #filter #history #rewrite #advanced