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 https://github.com/lynxbee/understanding-git-submodules.git repository should contain only README.superproject in it and nothing else.
[ same like, create empty git on github at, https://github.com/lynxbee/understanding-git-submodules.git
$ 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 https://github.com/lynxbee/understanding-git-submodules.git
$ git push origin master
$ mkdir workspace
$ cd workspace
$ git clone https://github.com/lynxbee/understanding-git-submodules.git
Same way, for understanding we created two remote repositories at https://github.com/lynxbee/submodule_one.git and https://github.com/lynxbee/submodule_two.git
Now, lets try to add submodule_one as our first submodule to superproject as,
$ git submodule add https://github.com/lynxbee/submodule_one.git
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 = https://github.com/lynxbee/submodule_one.git
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 <[email protected]> Date: Wed Mar 28 23:44:55 2018 +0530 Added submodule one Signed-off-by: Lynxbee Developer <[email protected]> commit bfaf1c699fbc8248a3eb085276ff0373fb400ac3 Author: Lynxbee Developer <[email protected]> Date: Wed Mar 28 23:36:07 2018 +0530 Superproject Signed-off-by: Lynxbee Developer <[email protected]> </[email protected]></[email protected]></[email protected]></[email protected]>
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 <[email protected]> Date: Wed Mar 28 23:40:14 2018 +0530 Create README.md </[email protected]>
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 https://github.com/lynxbee/submodule_two.git
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 <[email protected]> Date: Wed Mar 28 23:52:28 2018 +0530 Added second remote git as our another submodule </[email protected]>
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 https://github.com/lynxbee/understanding-git-submodules , it will look like as,