Skip to content

filter-repo Git Command Guide

The git filter-repo command is a modern, safe, and fast tool for rewriting Git history. It replaces the deprecated git filter-branch and provides better performance and fewer pitfalls. It can remove files, rewrite paths, anonymize data, and perform various history transformations.

Terminal window
git filter-repo [--path <path>] [--invert-paths] [--path-rename <old>:<new>] [--message-callback <command>] [--name-callback <command>] [--email-callback <command>] [...other options]
OptionDescription
—path Include/exclude paths from filtering
—invert-pathsInvert path selection
—path-rename :Rename file paths
—message-callback Modify commit messages
—name-callback Modify author/committer names
—email-callback Modify author/committer emails
—replace-text Perform text replacements across all files
—subdirectory-filter Extract subdirectory as new root
—commit-callback Custom commit modifications
—preserve-merge-parentsMaintain merge parent relationships
—forceOverwrite existing .git/filter-repo directory
—analyzeAnalyze repository and show filtering suggestions
ParameterDescription
(operates on current repository)Works on current Git repository
Terminal window
git filter-repo --path big-file.zip --invert-paths

Remove big-file.zip from all commits (keep everything except that file).

Terminal window
git filter-repo --subdirectory-filter my-project

Rewrite history to contain only my-project/ directory.

Terminal window
git filter-repo --path-rename old-dir/:new-dir/

Rename old-dir to new-dir in all files and commits.

Terminal window
git filter-repo --email-callback 'return email.replace(b"old@email.com", b"new@email.com")'

Replace author/committer email addresses.

Terminal window
git filter-repo --path-glob '*.log' --invert-paths

Remove all .log files from history.

Terminal window
git filter-repo --message-callback 'return message.replace(b"old text", b"new text")'

Replace text in all commit messages.

Terminal window
git filter-repo --analyze

Show suggestions for cleaning repository history.

Terminal window
git filter-repo --path unwanted-file.txt --invert-paths --preserve-merge-parents

Remove file while maintaining merge structure.

How do I remove a large file from Git history?

Section titled “How do I remove a large file from Git history?”

To remove a large file from history, use —path with —invert-paths:

Terminal window
git filter-repo --path big-file.zip --invert-paths

How can I extract a subdirectory as a separate repository?

Section titled “How can I extract a subdirectory as a separate repository?”

To extract a subdirectory, use —subdirectory-filter:

Terminal window
git filter-repo --subdirectory-filter my-subdir

To rename paths, use —path-rename:

Terminal window
git filter-repo --path-rename old-name.txt:new-name.txt

To change author info, use callback options:

Terminal window
git filter-repo --email-callback 'return email.replace(b"old@example.com", b"new@example.com")'

How do I preview what filter-repo will do?

Section titled “How do I preview what filter-repo will do?”

To analyze before running, use —analyze:

Terminal window
git filter-repo --analyze

To remove files by pattern, use —path-glob:

Terminal window
git filter-repo --path-glob '*.tmp' --invert-paths

How do I force overwrite an existing filter-repo directory?

Section titled “How do I force overwrite an existing filter-repo directory?”

To force filter-repo operations, use —force:

Terminal window
git filter-repo --force --path bad-file.txt --invert-paths

How does filter-repo handle merge commits?

Section titled “How does filter-repo handle merge commits?”

Use —preserve-merge-parents to maintain merge relationships:

Terminal window
git filter-repo --preserve-merge-parents --path removed.txt --invert-paths

Applications of the git filter-repo command

Section titled “Applications of the git filter-repo command”
  1. Removing sensitive files from repository history
  2. Extracting subdirectories as independent projects
  3. Cleaning up repository bloat from large files
  4. Rewriting author information for privacy
  5. Fixing repository structure and file organization
  6. Creating sanitized repositories for public sharing