An introduction to Git Submodule

Ashok Vishwakarma
Ashok Vishwakarma

Aug 22, 2017 • 1 min read

What is Git Submodule

We often require another plugin or library to be included into our projects to achieve faster development. The common way to do the same in using package manager like NPM, PIP, Composer or Bower depending on the chosen language or framework.

Git Submodule help you to treat your library or plugin as a separate project and can be pull, merge or checkout independently from your project.

Setup

To set-up Git Submodule you need to create .gitmodules file in your root directory.

Format of .gitmodules file

[submodule "<submodule_name>"]
path = <path_for_submodule>
url = <repo_url>
branch = <submodule_branch>
[submodule "app/lib/mysql"]
path = app/lib/mysql
url = https://github.com/ashokvishwakarma/phpmysql
branch = master

Run following commands to make it work.

git submodule init
git submodule update

This will add the reference to your .git/config file.

Push the changes to let git know about your submodules

git add . // add all changes to commit
git commit -m "<your_commit_message>" // commit the added files
git push origin <your_branch_name> // push the changes to origin

Reference

To read more about Git Submodules

Git - Submodules

git-scm.com

Git - git-submodule Documentation

git-scm.com

Please share this to help others.

Enjoy Git :)

Ashok Vishwakarma

Ashok Vishwakarma

Google Develover Expert — WebTechnologies and Angular | Principal Architect at Naukri.com | Entrepreneur | TechEnthusiast | Speaker