Friday, September 19, 2014

GIT: submodules

In this tutorial you will see examples of how to work with modules in GTI. It is a good practice for multiple projects to re-use common libraries of code, for example model library, APIs, utility classes, etc.

List Submodules



$ git submodule



Add submodule to an existing project

Before I start, I like to check what is the repository URL of the PARENT project, your submodules will likely have similar URL:

$ git config --get remote.origin.url

ssh://git@xyz.repositoryhosting.com/xyz/parent_project_name.git


In your (parent) project clone another project as submodule..

git submodule add --force ssh://git@xyz.repositoryhosting.com/xyz/submodule_name.git module/submodule_name
Cloning into 'module/submodule_name'...
remote: Counting objects: 47, done.
remote: Compressing objects: 100% (30/30), done.
remote: Total 47 (delta 6), reused 47 (delta 6)
Receiving objects: 100% (47/47), 1.85 MiB | 715.00 KiB/s, done.
Resolving deltas: 100% (6/6), done.
Checking connectivity... done.


If you checked out a base project and your submodule is empty, you need to initialize your submodules. The command below will bring the remote code to your local folders recursively for each submodule you have.

$ git submodule update -f --init --recursive


If the code you want in the submodule is in different branch then you have to checkout that branch

git submodule foreach git checkout develop


Finally you can do your normal PULL, or FETCH and REBASE 

$ git submodule foreach git pull


Remove submodule

List currently registered submodules


$ git submodule

$ git submodule deinit -f module/myOldModule
$ git rm -rf module/myOldModule
$ rm -rf 'module/myOldModule'