Managing the project with subproject — Git submodule

Yohan Malshika
3 min readSep 12, 2020

Hi all, Hope you all are doing well! If you are a developer who works on a large project, I think you already know about Git Submodule. This is one of the Git concepts that I learned from my internship at 99X. So today I am planning to tell you about Git Submodule.

Managing the project with subproject — Git submodule

In this article, I will tell you,

  1. What is Git submodule?
  2. Adding a submodule
  3. Cloning the repository with submodule
  4. Updating the submodule repository

What is Git SubModule?

If you are working on multiple repositories that share the same code, then you would probably need to have a repository within one. So for that Git has a concept called Submodule. The submodule is Git allows us to include repositories inside another repository. Basically, we can add a repository (share code) inside our main repository.

Adding a submodule

So guys we can add the project as a submodule to a repository(parent) like this,

git submodule add <git-remote-repo-url>

This will clone the submodule repository and add the information of the submodule to the main repository. This information describes which commit the submodule is pointing to. Also, the submodule’s code will not automatically be updated if the submodule repository updated. This situation has a good point. because our code might not work with the latest commit of the submodule, then it will prevent unexpected behavior.

Cloning the repository with submodule

When we clone the project with submodules, first we have to clone the main repository.

git clone <git-remote-repo-url>

Then it will clone the main repository and create the directories that contain submodules but it will create directories without any files of submodule repositories.

So for that we have to run two commands,

git submodule init

Then it will update the .git/config by the mapping from the .gitmodules file.

After it, we have to get all data of the submodule. so for that,

git submodule update

Then this will fetch all the data from the submodule and check out the mapped commit in the parent project.

Updating the submodule repository

We use the same data access layer for many projects. Then we update the data access layer in the project. and have to share changes with other projects. for that, first we have to commit the changes in the submodule and push it to the submodule and then we have to push the commits of the parent repository to the parent repository.

So when we update the inside the submodule repository project. First, we have to commit new changes to the submodule.

cd [submodule directory]
git add .
git commit -m “commit message”

After it, we go back to the main repository and check the current status of the main repository.

cd ..
git status

Now we can see new commits of submodules in the main repository. It will not show details about commits because that is the responsibility of the submodule.

Then we have to push the new commits. For that, first have to push the commits of submodule. After that, we have to commit changes and push the commits of the main repository.

cd [submodule directory]
git push -u origin <branch>

Then go back to the main repository and push the commits.

cd ..
git add .
git commit -m “commit message”
git push -u origin <branch>

So then developers can use the latest commits into other projects.

So That’s it for today. I think you learned something new from my article.

See you again soon with another article !!

Happy Coding 👽!!!

--

--