AlgoMaster Logo

git filter repo

10 min readUpdated December 6, 2025
Listen to this chapter
Unlock Audio

The ability to reshape a repository's history is a powerful tool in a developer's arsenal. Enter git filter-repo, a modern alternative to git filter-branch. While filter-branch can be slow and cumbersome, filter-repo offers a more efficient and flexible approach to modifying Git repositories.

Understanding how to leverage this command properly can help streamline your workflows, improve your repository’s structure, and even enhance security by removing sensitive data.

What is git filter-repo?

At its core, git filter-repo is a tool for rewriting Git history. It allows you to apply various filters on your repository, enabling you to modify commits, remove files, or restructure branches based on specific criteria.

Unlike filter-branch, which operates on a per-branch basis and can be complex to use, filter-repo is designed with speed and ease of use in mind. It utilizes a more efficient algorithm that directly manipulates repository data, making it faster for large repositories.

In essence, git filter-repo operates on the entire repository's history at once, rather than iterating through each branch individually. Understanding the underlying object model is key here; filter-repo works primarily with commits, trees, and blobs to achieve its tasks.

Installation and Setup

Before diving into git filter-repo, you need to ensure that it's installed. While it might be included in some package managers, the recommended way is to install it via Python’s package manager, pip. Here’s how you can do it:

After installing, you can check if it’s available by running:

This should display the installed version. Make sure you have Git installed, as filter-repo relies on it.

Basic Usage Examples

Understanding the basic commands and operations is essential to effectively using git filter-repo. Here are some common scenarios:

Remove a File from All Commits

Suppose you accidentally committed sensitive information in a file called config.env. You want to remove this file from the entire history. You can do this with the following command:

This command removes config.env and all its history from the repository. The --invert-paths flag indicates that you want to keep everything except the specified file.

Visualizing Changes

After running this command, you can verify the changes by inspecting the commit history:

You should no longer see any references to config.env.

Renaming a File Throughout History

If you've decided to rename a file consistently across your entire repository, git filter-repo can handle that too. For example, if you want to rename old_name.txt to new_name.txt, use:

This command effectively rewrites the entire history, replacing every instance of old_name.txt with new_name.txt.

Advanced Filtering Options

Beyond simple operations, git filter-repo provides a range of powerful filtering capabilities. Let's examine a few advanced use cases.

Filtering by Commit Message

If you want to remove all commits containing a specific phrase in the commit message, you can use:

This command checks each commit’s message, and if it contains the word "secret," that commit is skipped. This is particularly useful for cleaning up a history marred by sensitive or inappropriate messages.

Using Python for Custom Logic

The ability to use a Python callback in your filtering opens up unlimited possibilities. You can tailor the conditions to meet any criteria, such as the author of the commit or specific files modified.

Splitting a Repository

Imagine you have a monolithic repository containing multiple projects, and you want to split it into separate repositories. Here’s how:

This command filters out everything except the project1 directory and renames it to the root of the new repository. You can repeat this approach for other projects, giving each its own repository.

Why This Matters

Splitting repositories can improve collaboration by allowing teams to focus on their respective projects without the overhead of unrelated files. It also simplifies dependency management and versioning.

Handling Large Repositories

When working with large repositories, performance can become an issue. Here are some strategies to optimize the use of git filter-repo.

Use the --force Option

In some cases, filter-repo might refuse to operate due to safety checks. Using the --force option can override these checks:

Parallel Processing

For even larger repositories, consider using the --parallel option, which allows filter-repo to run multiple processes simultaneously, reducing execution time.

This can significantly speed up operations on repositories with a large number of commits or files.

Recovering from Mistakes

When manipulating Git history, mistakes can happen. Fortunately, git filter-repo has built-in mechanisms for recovery.

Creating a Backup

Before executing any filter commands, always create a backup of your repository. You can do this simply by cloning it:

This ensures you have a safe copy to revert to if something goes wrong.

Reverting Changes

If you find that your changes were not what you intended, you can restore your repository to its previous state using:

This cleans up the repository's references and allows you to access the previous state.

By leveraging git filter-repo, you can efficiently rewrite your repository’s history, handle sensitive information, and manage large projects with ease. Its flexibility and speed make it a crucial tool in any developer's toolkit.

Now that you understand how to use git filter-repo for various scenarios, you are ready to explore the next step in managing your repository's history.

In the next chapter, we will delve into the concept of rewriting history, uncovering advanced techniques that will empower you to control and optimize your project's evolution.