bashadvanced

Git Filter-Repo — Rewrite Entire History

Use git-filter-repo to remove secrets, rename authors, or extract subdirectories from Git history.

bash
# Install git-filter-repo (pip or brew)
pip install git-filter-repo
# or: brew install git-filter-repo

# Remove a file from ALL history (e.g. leaked secret)
git filter-repo --path secrets.env --invert-paths

# Remove a directory from all history
git filter-repo --path-glob 'build/*' --invert-paths

# Remove files by pattern
git filter-repo --path-regex '.*\.log$' --invert-paths

# Replace text in all files across history (e.g. API key)
git filter-repo --replace-text expressions.txt
# expressions.txt contains:
# sk-live-abc123==>***REDACTED***
# literal:password123==>***REDACTED***

# Change author/committer email
git filter-repo --email-callback '
  return email.replace(b"old@company.com", b"new@company.com")
'

# Extract a subdirectory into its own repo
git filter-repo --subdirectory-filter packages/shared
# Now the root IS packages/shared, history preserved

# Analyze repo (find large blobs, etc.)
git filter-repo --analyze
cat .git/filter-repo/analysis/blob-shas-and-paths.txt

# After filter-repo, force push to remote:
git push origin --force --all
git push origin --force --tags

Sponsored

GitHub Copilot

Use Cases

  • Removing accidentally committed secrets from history
  • Extracting subdirectories into standalone repos
  • Migrating email addresses after company changes

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.