# Git Submodules

## Creating a submodule

Creating a git submodule is pretty easy:

```
$ git submodule add [path_to_repo] [Name_of_Submodule]
```

In the below picture, you can see that I created a submodule from a remote repo and put it in the Submodules folder. &#x20;

![](broken-reference)

What I did was, I created a private repo called `TestSubmodules` on GitHub and then created another private repo `Repo2`.  I added `Repo2`  as a submodule to `TestSubmodules`  and as you can see, it moved `Repo2` into the \
`SubmoduleRepo folder`. &#x20;

I, however, could not add a submodule to a local repository that was on my computer without a remote.  (It was worth the test, for science!)

{% hint style="info" %}
If you have a older version of Git, you may have to run the below command to get it to download the contents of the submodule:

```
git submodule update --init --recursive
```

{% endhint %}

After commiting and pushing up to GitHub, you will see this:

![](broken-reference)

### Cloning a repo with a submodule

When cloning a git repo with a submodule, simply cloning with `git clone [path_to_git_repo]` will not pull any of the submodules. &#x20;

![Cloning a repository](broken-reference)

{% hint style="info" %}
You will see a folder for your submodule but it will be empty!&#x20;
{% endhint %}

Instead, use:&#x20;

```
git clone --recursive [path_to_git_repo]
```

![Cloning a repository with submodules](https://1970861158-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Ljn7Z64iRi1UjaYmuws%2F-LjsEC_4rs18RdODGLS9%2F-LjsF84kTFLpo0j2zbAt%2Fsubmodule2.PNG?alt=media\&token=c3a4f763-aee4-4a66-890d-0a047f764d57)

This will pull your submodules as well. &#x20;
