Skip to main content
One of the most common uses of git-filter-repo is extracting a subdirectory from a repository to create a standalone project or prepare it for merging into another repository.

Basic Extraction

To extract a single directory and make it the root of your repository:
1

Clone the repository

Start with a fresh clone of your repository:
git-filter-repo requires a fresh clone as a safety measure. Use --force to override this requirement if absolutely necessary.
2

Extract the subdirectory

Use the --subdirectory-filter option to extract a directory:
This will:
  • Keep only files under src/
  • Move src/ contents to the repository root
  • Remove commits that only touched files outside src/
3

Verify the results

Check that the extraction worked correctly:

Extract and Rename to Subdirectory

For merging into another repository, you may want to extract a directory but place it under a new path:
This command:
  • Extracts only the src/ directory
  • Places it under my-module/src/ in the filtered repository
  • Preserves the full history

Advanced: Extract and Rename

Extract a specific subdirectory and rename it to a different path:
This keeps src/some-folder/some-feature/ but renames it to src/.

Complete Example: Prepare for Merging

When preparing a subdirectory to merge into another repository:
1

Extract and reorganize

This command:
  • Extracts the src/ directory
  • Moves it to my-module/src/
  • Prefixes all tags with my-module- to avoid conflicts
2

Add the remote

3

Merge the history

Always work on a fresh clone. git-filter-repo rewrites history and will remove your remote configuration to prevent accidental pushes.

Extracting Multiple Paths

To extract multiple directories or files:
All specified paths will be retained while everything else is removed.