paint-brush
Learn How To Use Git Submodules on Practiceby@nabheetmadan
267 reads

Learn How To Use Git Submodules on Practice

by Nabheet MadanJune 17th, 2021
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

We were having multiple projects in a folder and all pointing to its own remote git repo. We thought of making our parent folder also available on GitHub, so that all projects are available at one place. We were expecting everything to be available on remote repo but sadly that is where the real problem starts. So in this post we will try to demystify and resolve this situation by learning something new. It also leads you into a direction which is called as Submodules. Sub modules are nothing they allow you to have repo inside repo.
featured image - Learn How To Use Git Submodules on Practice
Nabheet Madan HackerNoon profile picture

Situation

We were having multiple projects in a folder and all pointing to its own remote git repo. we thought of making our parent folder also available on GitHub, so that all projects are  available at one place. We went ahead with our typical regular follow as mentioned below

Initializing the parent folder with below mentioned command

git init

Then as usual staging all content and commiting  using below mentioned command

Git add .
Git commit  -m “Main Project”

Created a repo on Github and added the remote connection uploaded the stuff.

git branch -M main
git remote add origin <url>
git push -u origin main

We were expecting everything to be available on remote repo but sadly that is where the real problem starts. If you look at screenshot below we can see two projects but none of them is showing any contents. So in this post we will try to demystify and resolve this situation by learning something new.

For people who like to watch instead of reading can checkout this video

Journey towards finding the solution.

We were surprised by this, so thought of checking again the steps executed so far. That is where we got our first hint. It was a warning message and as always, we have ignored it.

It clearly highlights in case you have repo embedded in repo it will not upload all the contents of the inner repo. Good thing is it also leads you into a direction of a concept which is called as Submodules. Submodules are nothing they allow you to have repo inside repo. Normally while developing project we tend to refer multiple external libraries, which themselves are versioned one. So in order to use those the concept of submodule was brought in.  So here lie the answer to our problem.

So as a first step we need to add the submodules to our parent class. Lets do it using

Git submodule add <url> <name of module>

We have added our both projects as submodules, and you can see a new  file .gitmodule is created. On checking its content it contains details of all the submodules.

Once that is done now we follow our regular commit cycle.

Git add .
Git commit  -m “Main Project”
git branch -M main
git remote add origin <url>
git push -u origin main

Lets see how does the remote repo looks like. Wow it contains an extra link which point to the other repositories

Okay so our core issue is resolved but we thought of exploring it further as we found this concept very powerful.

Exploring Further

How do we clone?

We initially thought we can clone with simple git clone command but sadly all we end up is with empty folders like earlier as see in screen shot.

On searching further we came to know of –recursive option which can help us in copying also the submodules.

Git clone <url> --recursive. 

How do we check status of all projects at one go from parent folder?

We were thinking in case we need to check status of each project, we will need to go under each folder which is cumbersome. So does submodule help in this and it does all you need to do is

Git submodule foreach git status

If we can check status can we pull also? Yes we can using below command

Git submodule foreach git pull

I hope you have learnt something new from this. Would love to hear your set of learnings while using git.

Previously published at https://blogs.sap.com/2021/06/16/crazy-case-of-embedding-git-repositories/