DEV Community

Cover image for Crazy case of Embedding Git Repositories
Nabheet Madan
Nabheet Madan

Posted on

Crazy case of Embedding Git Repositories

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
Enter fullscreen mode Exit fullscreen mode

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

Git add .
Git commit  -m “Main Project”
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.
Situation we are in

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.
Warning Message
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>
Enter fullscreen mode Exit fullscreen mode

We have added our both projects as submodules, and you can see a new file .gitmodules is created. On checking its content it contains details of all the submodules.
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
Enter fullscreen mode Exit fullscreen mode

Lets see how does the remote repo looks like. Wow it contains an extra link which point to the other repositories
Correct Repo
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.
git clone without recursive
On searching further we came to know of –recursive option which can help us in copying also the submodules.

Git clone <url> --recursive. 
Enter fullscreen mode Exit fullscreen mode

git clone with 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
Enter fullscreen mode Exit fullscreen mode

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

Git submodule foreach git pull
Enter fullscreen mode Exit fullscreen mode

Pull

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

Top comments (0)