First, what is submodule? Well, eventually, almost all of projects have dependencies on some kind of libraries. Most of these libs comes as binary files, but sometimes it comes as source code (git repository). Submodule comes for help here. Submodule embeds one project into another. So that the outer project can access embedded one directly.

Add a submodule

First, get the GitHub path from the project you want to embed, then go to target project . Run commands:

git submodule add git@github.paypal.com:<user>/project.get <name>

The <name> parameter is optional, it designates the name of the folder of submodule in the outer project

After execution, you will see a file called .gitmodules. You may not able to see it if you use linux machine since it starts by dot. You can still open it in terminal or text editor. You will see something like that:

[submodule "refs"]
path = refs
url = git@github.paypal.com:/project.git

The refs here is the name parameter in above command

So far, you still can not see actual files in the project. In order to be able to "import" the project, you need to run

git submodule update --init --recursive

If everything looks good, you will find a folder called "refs" (I use refs as name parameter) with all project files rest inside. Now you can use these files in the outer project.

Tips: You can add multiple submodules as you like as long as you specify different name parameters for them.

Update submodule

For some reason, if you want to change your submodule repo, you can use

git submodule set-url <name> git@github.paypal.com:<user>/project.get

# delete current folder
git rm ref

# sync with remote
git submodule update --remote

Tips: For some reason, I haven't update submodule successfully. The recommendation is delete the submodule, and then add the new one with different name

Delete submodule

Finally, we are here talking about deleting submodule. For whatever reason you want to delete, it is quite simple, just run commands below

git rm <name>
rm -rf .gitmodules
vim .git/config
# delete anything related with submodules in config file if there is any. Then save and exit

Conclusion

It is a little bit strange to use git submodule, but it is still handy to know it. With these knowledges, I believe you can handle with it if you see it in your projects.

How to use git submodule