This write-up will explain:
What are Nested Git Repositories?
Nested Git repositories are repositories that are stored inside other Git repositories. These repositories are created when a project depends on another project, and the dependent project is included as a submodule or subtree within the main project’s repository. While working on a nested repository, the changes will not be reflected in the parent repository until the user commits and pushes the changes in the nested repository, then updates the parent repository.
How to Create Nested Git Repositories?
There are two methods to create nested Git repositories, such as:
- Method 1: Create Nested Git Repository Using “git submodule” Command
- Method 2: Create Nested Git Repository Using “git subtree” Command
Method 1: Create Nested Git Repository Using “git submodule” Command
To create a nested Git repository, first, navigate to the desired local repository and run the “git submodule add <repository-URL> <submodule-name>” command:
Then, view the newly added submodule using the below-stated command:
It can be observed that the submodule (nested repository) has been created named “Demo_SubMod”:
Method 2: Create Nested Git Repository Using “git subtree” Command
Another way to create a nested Git repository is to run the “git subtree add –prefix <directory-name> <repository-URL> <branch-name>” command:
Here, the “–prefix” is added to create a nested repository named “subtreeDirectory” into which you want to pull subtree:
Then, list the subtree with the help of provided command:
As you can see that the subtree (nested repository) has been added successfully:
We have explained nested Git repositories and the methods to create them.
Conclusion
A nested repository is a git repository that is located within another Git repository. In these types of repositories, one project depends on another project. The dependent project is included as a subdirectory within the main project’s repository and is considered a nested repository. The nested repository can be created using the “git submodule” or “git subtree” command. This write-up explained about the nested Git repositories.