Your First Filtering Operation
This guide walks you through a complete example of using git-filter-repo to extract a subdirectory from a repository and prepare it for merging into another project.git-filter-repo is designed to work on a fresh clone for safety. It will refuse to run on a repository with uncommitted changes or unpushed commits unless you use
--force.Example Scenario
Let’s say you want to:- Extract only the
src/directory from a repository - Move everything into a new
my-module/subdirectory - Rename all tags with a
my-module-prefix to avoid conflicts
Analyze your repository (optional)
Before filtering, you can analyze your repository to understand its structure:This creates a
.git/filter-repo/analysis/ directory containing reports about:- Largest files
- Most commits per path
- File and directory sizes
- Extensions used
Run the filter operation
Now perform the actual filtering with a single command:Let’s break down what each option does:
Keep only files under the
src/ directory. Everything else is removed.Move all remaining files into a new
my-module/ subdirectory. So src/foo.c becomes my-module/src/foo.c.Rename all tags by prepending
my-module- to prevent naming conflicts. The empty string before the colon is the old prefix to replace.How Fast Is It?
One of git-filter-repo’s key advantages is speed. For reference:filter-repo
Minutes for large repos
filter-branch
Hours to days for the same repos
Common Quick Operations
Here are more examples you can try:Remove a file from all history
Remove a file from all history
Remove a sensitive file that was accidentally committed:The
--invert-paths flag means “remove” instead of “keep”.Extract a subdirectory as the new root
Extract a subdirectory as the new root
Make a subdirectory the root of the repository:Everything in
path/to/subdir becomes the root, and everything else is removed.Remove files by pattern
Remove files by pattern
Remove all
.log files from history:Replace text in all files
Replace text in all files
Replace text across all files in history:Or use a file with multiple replacements:
Remove large blobs
Remove large blobs
First analyze to find large files:Then remove them:
Command Comparison
- filter-repo
- filter-branch
- BFG
Safety Features
git-filter-repo includes several safety features:Fresh Clone Check
Refuses to run unless in a fresh clone (override with
--force)Remote Removal
Removes git remotes by default to prevent accidental pushes of rewritten history
Empty Commit Pruning
Automatically removes commits that become empty after filtering
Auto Cleanup
Automatically runs garbage collection to remove old objects
What Happened to My Repo?
After running git-filter-repo:History is rewritten
All commit hashes change because the repository history has been rewritten. Old commit references are invalid.
Remotes are removed
The
origin remote is removed by default. This prevents accidentally pushing rewritten history.Add it back if needed:Next Steps
Common Use Cases
Explore more filtering scenarios
Path-Based Filtering
Learn advanced path filtering techniques
Command Reference
See all available options
Migration Guides
Migrate from filter-branch or BFG
Getting Help
If you run into issues:FAQ & Troubleshooting
Find answers to common questions
