How to create git submodule superproject and add different remote gits as its submodule repositories

It often happens that while working on one project, you need to use another project from within it. Perhaps it’s a library that a third party developed or that you’re developing separately and using in multiple parent projects. A common issue arises in these scenarios: you want to be able to treat the two projects as separate yet still be able to use one from within the other.

For understandting this, lets start by creating 3 simple repositories which contains only README’s into it onto github. [ You can create it in your service choice ]

To start with repository should contain only README.superproject in it and nothing else.

[ same like, create empty git on github at,
$ git init
$ echo “This helps to understand how git submodules are used” > README.superproject
$ git add README.superproject
$ git commit -a
$ git remote add origin
$ git push origin master

 $ mkdir workspace 
 $ cd workspace 
 $ git clone 

Same way, for understanding we created two remote repositories at and

Now, lets try to add submodule_one as our first submodule to superproject as,

 $ git submodule add 

Now, if we check with git status command, we will see that in superproject one .gitmodules file and one submodule_one folder will get created.

$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed: (use "git reset HEAD ..." to unstage)	new file: .gitmodules	new file: submodule_one

if we check the contents of .gitmodules, it will inform us that we have added one submodule as,

$ cat .gitmodules
[submodule "submodule_one"]	path = submodule_one	url =

Lets add and commit this files as,

 $ git add . 
 $ git commit -a -s 

Now, if we see the git log in superproject directory, it will look like as below,

 $ git log
commit f3cff866a1a41e9d08c47ba09e8f92dcb70aef42
Author: Lynxbee Developer Date: Wed Mar 28 23:44:55 2018 +0530 Added submodule one Signed-off-by: Lynxbee Developer commit bfaf1c699fbc8248a3eb085276ff0373fb400ac3
Author: Lynxbee Developer Date: Wed Mar 28 23:36:07 2018 +0530 Superproject Signed-off-by: Lynxbee Developer 

Now, since we have added one more git repository into superproject, lets see how the git log will be seen in submodule directory as,

 $ cd submodule_one/ 
$ git log
commit bcf736c3d3ec573108d1c5f2b8dc98ed9cacea1e
Author: Linux Developer Date: Wed Mar 28 23:40:14 2018 +0530 Create

As, we can see the submodule_one directory only shown the logs relevant to this git repository and it didn’t shown any logs from the superproject. This is the very important and good feature why we need to use git submodules since it allows us to maintain one git as part of another git and still both gits are independent. This makes life much easier for working with big projects which use multiple existing opensource libraries which residers into difffernt distributed git servers.

Now, Lets try to add another git from remote location/URL as our second submodule as,

 $ git submodule add 

Here also, we have to follow same procedure to add commit this submodule and push details to superproject as,

 $ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed: (use "git reset HEAD ..." to unstage)	modified: .gitmodules	new file: submodule_two
$ git add . 
 $ git commit -a 
 $ git log
commit 3bfd97573295852f34a73dbfb1786e84110caa17
Author: Lynxbee Developer Date: Wed Mar 28 23:52:28 2018 +0530 Added second remote git as our another submodule

Here, we can see that we added, another remote git as second submodule.

Now, push this added second submodule to superproject as,

$ git push –all

Now, if we check the superproject at github , it will look like as,


READ  How to fix - fatal: Unable to create '.git/shallow.lock': File exists.

Leave a Reply

Your email address will not be published. Required fields are marked *