What are submodules in git?
Git allows you to include other Git repositories inside a repository called submodules. Git allows you to commit, pull and push to these repositories independently.
How does it work?
Like we have two modules A and B both have different .git files but now the requirement is to add both inside one repository but it’s not possible simply if we create one repository and push module A there then we can’t push module B on the same remote URL.
But we have Submodules concept in GitHub it means we can add both repositories with different .git files inside the parent repository.
ADD multiple repositories inside a parent repository
git submodule add <git-remote-repo-url>
You can 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 behavior.
After adding remote URLs hit below commands.
git add .
git commit -m “pushed submodules”
Now when you start work on module A will push commits from that repository only but it won’t update the parent repository still the main repository will point to the old commit.
And we can’t just pull the latest commits like we normally do with a single repository.
Then, how can we update the parent repository of submodules?
just hit below command to update the main repository with latest commits now main repository head will point the new commit.
git submodule update
git submodule update — remote
How it’s helpful?
Microservices 💯 🤔
Yes in terms of Microservices it’s very helpful we can add multiple services repository inside one main repository easy to manage.
Microservices are still pretty popular in our industry since they imply lots of benefits. However, working with that kind of architecture might be annoying when it comes to committing the changes using GIT. Why? Simply because in a common approach, each microservice should be put in a separate repository. Therefore, instead of doing your typical „GIT sequence” once, you end up with few terminals where you do pretty much the same job. Fortunately, you can avoid that with no big deal.
I have created a demo repository for this on GitHub.
Thanks for reading 🎉🎊 and I hope you liked this article. Don’t forget to clap and follow me for upcoming articles.