Using Submodules.
·In this post we will learn about frequently used method to include Submodules in github repository.Before learning submodules you should know the dependency management.
What is dependency management ?
While making API if it depends upon several other modules it is necessary to include them in one in order to ensure proper functioning of API.This is especially done when bigger project are involved and all dependency should be kept on one level. Dependency tool is available for dependency management.
What are submodules ?
A submodule in a git repository is like a sub-directory which is really a separate git repository in its own right. This is a useful feature when you have a project in git which depends on a particular versions of other projects.
Here are some languages and thier dependency management ;
- Perl has CPAN
- PHP has Composer, Packagist and good ol’ Pear
- Python has PyPI
- Ruby has Bundler and Rubygems
- Rust has Crates
And many more examples can be found.
You should have proper version of git.It is important to have proper git version. When you add a submodule in Git, you don’t add the code of the submodule to the main repository, you only add information about the submodule that is added to the main repository. This information describes which commit the submodule is pointing at. This way, the submodule’s code won’t automatically be updated if the submodule’s repository is updated. This is good, because your code might not work with the latest commit of the submodule, it prevents unexpected behaviour.
How do we add submodule ?
How to get submodule ?
submodule add command adds new .gitmodule file and the repository which is included as submodule.Then to commit changes in repository
Submodules or Subtree ,Confuse ?
Many of us may have confusion regarding submodule and subtree. we have seen submodule above now we will see how to make subtree.
you can consider subtree as copy or integrating another repository into your own repository. The subtree command adds the the submodule-name to your own repository. Usually this approach is not used as it takes more space in your repository .Consequently ,using submodule creates link to the repository which user may or may not use it depends on user whether to use submodule or just use the source code in repository(If the submodule is optional).
How submodules are represented ?
This below information I found useful on Mark’s blog.So I thought it might be useful for others.More information can be found on his blog
Various file object such as blob -> Indicates File. tree -> Subdirectory. commit -> Indicates Submodule 1e38e022c1c7d27f6dd9b765793087b59d147ef8 -> SHA1 hash
How to see status of submodules ?
To view status of submodule we use just git submodule without any arguments.
If –recursive is specified, this command will recurse into nested submodules, and show their status as well.If your repository has multiple embeded submodules.
You can see each SHA-1 hash begins with a space, a ‘+’ or a ‘-‘ which indicate the following things:
+ The version checked out in the submodule is different from that specified in the super-project. The object name shown is that of the commit that the submodule is currently at.
– The submodule hasn’t been initialized or you may not have committed the changes in github repository.
U This option indicates if the given module has merge conflicts.
[space] The submodule’s HEAD is at the correct version .
More can be learnt regarding submodules.
Downside of using submodule
One of the advantage that dependency manager such as Apache ivy has transitive dependency resolution.It downloads already compiled packages. straight from Apache ivy pages With Apache Ivy, it’s different: simply write a dependency file once for the component, and benefit from the work already done anytime this component is reuse.