Introduction To Git Submodule

What is Git Submodule ? 

 
It is a very common situation that the project you are working on has dependency on another project. This dependency can be in the form of source code dependency. You might need source code of another project in your project and you want to build it together to generate executables.
 
In such scenarios, usually we end up writing some copy scripts to copy the source code of one project into a main project. This can be tiresome and can get complicated over the time when you have very complex build steps.
 
This problem can be easily be solved by using Git - Submodule.
 

How to use Git Submodule ?

 
It is fairy simple to use Git Submodule. Consider a scenario where you have a main project called "test-utility" and you want the source code of another lib called "my-lib" in your project. You can just follow the below-mentioned steps to accomplish this with the help of the Git submodule feature.
 
Add the submodule link of library project (my-lib) to your main project (test-utility).  
  1.   git submodule add https://github.com/jaykumar-parmar/my-lib.git
Git Submodule
 
This command will create my-lib folder to your code repository and also clone all the content of "my-lib" repository.
 
It also creates .gitmodules file which keeps track of all submodules added to the project repository.
 
Git Submodule
 
Push .gitmodule file to Git, so that next time when some other developer clones the repository they will also get source of "my-lib" repository. Once pushed you can see submodule added to Git repository.
 
Git Submodule
 
Anytime you need  to get updates from submodule repository, give the following command.
  1.   git submodule update --remote
For more information refer here.
 
Hope this was informative.