Git

What are Nested Git Repositories?

Developers deal with multiple projects while working on Git. Usually, it is tough to manage large projects when all code exists in a single repository. In this situation, Git permits developers to break a project into small and nested repositories. Moreover, sometimes, one project depends on another project, so it can be useful to include the dependent project as a nested repository within the main project.

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:

$ git submodule add https://github.com/laibayounas/demo.git Demo_SubMod

Then, view the newly added submodule using the below-stated command:

$ ls

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:

$ git subtree add --prefix=subtreeDirectory https://github.com/laibayounas/demo.git master

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:

$ ls

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.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.