Crazy case of Embedding Git Repositories
Tags: git, submodules, javascript, node
Published: 2021-06-16 10:04:57

---
title: Crazy case of Embedding Git Repositories
published: true
description: In this post we learn about concept of Submodules in git by actually troubleshooting a case of embedding multiple git repositories
tags: git,submodules,javascript,nodejs
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eyued7znq4dmf4ehtsm4.jpg
---
## 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.
![Situation we are in](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8bb55q1dijoo9jaflw03.PNG)

For people who like to watch instead of reading can checkout this video
{% youtube ZNVWVytzCZw %}

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gley8h7i2ard8uvjf00l.PNG)
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](https://git-scm.com/book/en/v2/Git-Tools-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 .gitmodules is created. On checking its content it contains details of all the submodules.
![submodules](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fsyssj1xfrw4ntjxl8up.PNG)
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
![Correct Repo](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hsc0uaswzyf61ggsok36.PNG)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o9t3i5q4kchsdwj05yp5.PNG)
On searching further we came to know of –recursive option which can help us in copying also the submodules.
```
Git clone <url> --recursive. 
```
![git clone with recursive](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t1eue637ps1h8vyferxl.PNG)
### 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
```
![Status](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qs4cgr8ssubl2wzqiin7.PNG)
If we can check status can we pull also? Yes we can using below command
```
Git submodule foreach git pull
```
![Pull](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/623gqhh7e3uwhck6ffg4.PNG)

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