Detach move subdirectory into separate Git repository
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Managing large Git repositories can often lead to complexities, especially when dealing with vast codebases spread across multiple directories. A common solution to streamline and organize a project is to separate a subdirectory into its own Git repository. This article provides a comprehensive guide on how to detach a subdirectory from an existing Git repository, maintain its historical commits, and set it up as an independent repository.
Why Detach a Subdirectory?
Before diving into the technical details, it's essential to understand the reasons for detaching a subdirectory:
- Modularity: By keeping code modular, different teams can manage and release smaller components independently.
- Performance: Reducing the size of a repository can lead to faster operations such as cloning, fetching, and checking out branches.
- Focus: Separate repositories allow for dedicated issue tracking, isolated versioning, and individual maintenance schedules.
Steps to Detach a Subdirectory
1. Clone the Existing Repository
Begin by cloning the existing repository. If it's already cloned, ensure that the working directory is clean:
2. Use git filter-branch (or git filter-repo)
To extract the subdirectory while preserving its commit history, you can use the git filter-branch command. However, due to its complexity, the recommended tool is git filter-repo.
First, ensure git filter-repo is installed. For installation, you can follow the guidelines from its GitHub repository.
Navigate to the repository's root and execute:
3. Clean Up the Resulting Repository
After filtering, verify the contents:
Ensure that only files from the desired subdirectory are present. If everything is satisfactory, commit the changes:
4. Remote and Push to New Repository
Now that the subdirectory stands as its own repository, set it up on a new remote repository platform (e.g., GitHub, GitLab). After creating the new repository online, add it as a remote:
5. Document the Change in the Original Repository
In the original repository, replace the extracted subdirectory with a reference to the new repository. You might consider using Git submodules:
Advanced Topics
Retaining Multiple Branches
If the original repository has multiple branches that need preservation, repeat the filtering and cleaning process for each branch:
Handling Large Repositories
For substantial repositories, consider automating the workflow using shell scripts or tools like git-fast-filter.
Key Summary
The following table summarizes the key steps and outcomes when detaching a subdirectory:
| Step | Action | Outcome |
| 1. Clone Repository | git clone <url> | Local copy of the repository. |
| 2. Filter Subdirectory | git filter-repo --subdirectory-filter | Subdirectory is isolated with full commit history. |
| 3. Clean Up | git add . && git commit -m | Prepared the repository for remoteness. |
| 4. Push to Remote | git remote add origin <url>
git push | Subdirectory is now a standalone repository. |
| 5. Document Change | git submodule add <url> | Main repo maintains linkage to the new subdirectory repository. |
Conclusion
Detaching a subdirectory into a separate Git repository can bring significant benefits, from improving repository performance to enhancing project modularity. While this task may initially seem daunting, following the outlined steps ensures a smooth transition. As always, careful planning and understanding of requirements are advised before initiating such changes.
By consolidating components into individual repositories, developers can foster clearer project organization and enhanced collaboration among teams.

