Overview
Path-based filtering allows you to rename files and directories throughout your repository’s entire history. This is essential when restructuring projects, merging repositories, or correcting naming conventions.Path Renaming Basics
Simple Rename
Rename a directory throughout history:Trailing slashes are optional for directories:
tools/:scripts/(with slash)tools:scripts(without slash)
Multiple Renames
Perform several renames in one operation:Advanced Renaming
Merge Directories
Rename multiple directories to the same location:- Before
- Command
- After
Handle Naming Conflicts
If two files would collide when renamed, specify an additional rename:Specific file renames should come before directory renames to prevent conflicts.
Combining Filtering and Renaming
Filter Then Rename
Path renaming doesn’t filter paths - combine with--path for that:
- Wrong
- Right
Create New Structure
Restructure your repository layout:Restructuring Projects
Flatten Directory Structure
Move files from nested directories to root:Create Nested Structure
Add directory layers:: means repository root.
Module Reorganization
Reorganize a monorepo into separate modules:Merging Repositories
Prepare Repository for Merge
When merging multiple repositories, prefix each with a unique directory:- Repo A
- Repo B
- Merge
Split Monorepo
Extract a module and rename it:Regex-Based Renaming
Use regex for complex renaming patterns:Regex Syntax:
(.*)- Capture group (any characters)[^/]*- Non-slash characters\1,\2,\3- Back-references to captured groups
Complex Renaming Examples
Normalize File Extensions
Add Version Prefix to Directories
Reorganize by Language
Group files by programming language:Renaming with Callbacks
For extremely complex renames, use Python callbacks:Important Considerations
Silent OverwritesIf you rename a path to one that already exists, the existing file is silently overwritten. Use
--analyze to check for potential conflicts first.Partial Renames
Rename paths only on specific branches:Verification
After renaming, verify the results:Next Steps
- Learn about Content-Based Filtering
- Explore Commit Message Rewriting
- See Library Usage for programmatic control
