Git

What is the Difference Between Git Submodule and Subtree?

Git is a tool that allows multiple developers to easily work on the same project simultaneously while keeping track of all changes made to the code over time. However, if the project is large, it becomes hard to handle because it contains many files and branches. In this situation, Git permits users to separate a large Git repository into small modules. Moreover, you can also integrate one repository with another.

This study will explain:

What are the Main Differences Between Git Submodule and Subtree?

A “Git submodule” can include one Git repository within another. It allows users to contain a Git repository as a subdirectory of another Git directory and make or commit changes independently of the parent repository. It retains its own identity, including all history, tags, branches, etc. On the other hand, the “Git subtree” is a way to merge one repository’s content into another. It retains its own identity, but its commits are linked with the commits of the parent repository.

How to Create/Add the Submodule in Git?

To add a submodule in Git, first, navigate to the desired Git repository. Then, execute the “git submodule add <repository-URL>” command. To do so, follow the below-provided instructions.

Step 1: Navigate to Local Repository

First, redirect to the particular Git repository with the help of the given-provided command:

$ cd "C:\Git\ReposC"

Step 2: Add Submodule

Then, run the “git submodule add” command along with the desired local or remote repository’s URL to add the submodule. For instance, we have added the GitHub repository’s URL:

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

Step 3: Verify Changes

To ensure whether the submodule has been created or not, type out the following command:

$ ls

According to the given screenshot, it can be seen that the submodule “demo/” has been created successfully:

How to Create/Add the Subtree in Git?

To add a subtree in Git, first, move to a particular directory. Then, utilize the “git subtree add –prefix <directory-name> <repository-URL> <branch-name>” command.

Step 1: Move to Local Git Repository

First, type out the below-given command and redirect to the required local repository:

$ cd "C:\Git\ReposA"

Step 2: Add Subtree

Then, execute the “git subtree add” command, specify the prefix, remote repository URL, and remote branch name to add the subtree:

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

Here, the “–prefix” is used to create a local directory named “subtreeDirectory” into which you want to pull subtree:

Step 3: Verification

For the verification, list of the repository content:

$ ls

As you can see that the “subtreeDirectory/” subtree has been added successfully:

That was all about the Git submodule and subtree.

Conclusion

The main difference between a Git submodule and a subtree is that it retains its own identity and can be updated or committed independently of the parent repository. Whereas a subtree is merged into the parent repository, and its commits are linked with the commits of the parent repository. This write-up explained the difference between a Git submodule and a subtree.

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.