How To Move Subdirectory Into Separate Git Repository?
Last Updated :
26 Jun, 2024
When working with large monolithic Git repositories, teams often find that separating parts of the codebase into distinct repositories can improve maintainability and focus. This article explains How To Move Subdirectory Into Separate Git Repository while preserving its history.
Steps To Move Subdirectory
Step 1. Extract the Subdirectory
First, we need to filter out the history of the subdirectory to be moved. This can be accomplished with git filter-repo, which is more modern and efficient than the older git filter-branch command.
Follow these steps:
1. Install git-filter-repo
If git-filter-repo is not installed, you can install it via pip:
pip install git-filter-repo
2. Filter the Repository
Let's say we have a repository with the following structure:
my-repo/
├── .git
├── subdir/
│ ├── file1.txt
│ └── file2.txt
└── other-dir/
└── file3.txt
To extract the subdir into its own repository:
1. Clone the repository (if you don’t already have a working copy):
git clone https://github.com/username/my-repo.git
cd my-repo
2. Use git-filter-repo to filter the subdir:
git filter-repo --path subdir --path-rename subdir:.
Here, --path subdir tells Git to keep only the history for subdir, and --path-rename subdir:. renames the subdir path to the root (.) of the repository.
Note: This command will alter the repository in place. If you want to keep the original repository unchanged, use a separate clone.
Step 2. Create the New Repository
Now, we will create a new repository to host the subdir content.
1. Go to the directory where you want to create the new repository:
cd ..
mkdir new-repo
cd new-repo
2. Initialize the new repository:
git init
3. Add the Subdirectory's Content
Next, we need to move the filtered content from the original repository to the new one.
1. Copy the .git directory from the filtered repository to the new repository:
cp -r ../my-repo/.git .
2. Reset the working directory to match the history:
git reset --hard
This makes the current working directory reflect the state of the subdir from the original repository's history.
3. Clean up the original repository (if necessary):
rm -rf ../my-repo
4. Push to Remote (if needed)
If you want to push this new repository to a remote server (e.g., GitHub), follow these steps:
1. Add a remote repository:
git remote add origin https://github.com/username/new-repo.git
2. Push the changes:
git push -u origin master
Optional: Update Submodules in the Original Repository
If you want the original repository to use the new repository as a submodule:
1. In the original repository, remove the old subdirectory:
rm -rf subdir
2. Add the new repository as a submodule:
git submodule add https://github.com/username/new-repo.git subdir
git submodule init
git submodule update
Conclusion
Moving a subdirectory to a new Git repository can help streamline project management and development processes. By using git-filter-repo, you can extract and preserve the history of the subdirectory, ensuring continuity. Always back up your data before performing such operations to safeguard against potential mishaps.
Similar Reads
How to Clone Only a Subdirectory of a Git Repository?
In some scenarios, you may need to work with only a specific subdirectory of a large Git repository. Unfortunately, Git does not support cloning a subdirectory directly. However, there are a few effective workarounds to achieve this, including using sparse checkout or exporting the subdirectory. Thi
3 min read
How to Push Git Tags to Remote Repository?
Git tags are references to specific points in Git history and are used to mark release points (like version numbers). Pushing Git tags to a remote repository is important for sharing these release points with collaborators or deploying releases to production environments. In this article, we'll expl
3 min read
How To Reset Remote Repository to a Certain Commit in Git?
Resetting a remote repository to a specific commit in Git can be an important task, especially when you need to revert changes or roll back to a stable state. This article will guide you on how To Reset Remote Repository to a Certain Commit in Git. Table of Content Approach 1: Using `git reset` and
2 min read
How to Merge two Git Repositories?
Merging two Git repositories can be a complex task, especially if they have distinct histories, branches, and commit structures. This guide explores different approaches and details each step involved in merging two Git repositories. Merge One Repository as a Subtree of AnotherThis approach involves
1 min read
How to Reset a Git Branch to a Remote Repository?
Resetting a Git branch to match a remote repository is a common task, particularly when you want to discard local changes and make your branch identical to the remote counterpart. This can be useful in scenarios where your local branch has diverged from the remote, and you want to synchronize it wit
3 min read
How to Install Git on Raspberry Pi?
Git is a widely used version control system that allows developers to track changes in their code and collaborate effectively. Installing Git on a Raspberry Pi helps you manage your projects directly from this versatile and affordable device. This guide will walk you through the steps to install Git
2 min read
How To Clone a Git Repository Into a Specific Folder?
Cloning a Git repository is a fundamental task for developers, allowing you to create a local copy of a remote repository. While the default behavior places the cloned repository into a directory named after the repository itself, there are instances where you might want to specify a different direc
3 min read
How to Revert Local Changes in Git Repository to a Previous State?
Git is the foundation upon which all version control systems are built and it helps developers manage their changes. However, people always make mistakes and you may want to discard your local coding changes. This article will explain different approaches to reverting your local hacks back to your g
3 min read
How To Change The Remote Repository For Git Submodule?
Git submodule allows to include a Git repository as a subdirectory within another Git repository. This is useful to manage dependencies or keep separate components of a project in their own repositories. But, there may be times when we need to change the remote repository that a submodule points to.
4 min read
How to Remove .DS_Store Files From Git Repositories?
If you're a macOS user, you've probably encountered .DS_Store files. The macOS Finder creates these files to store custom attributes of a folder, such as the position of icons and the view settings. While they are harmless in themselves, they can clutter your Git repository and cause unnecessary con
3 min read