Git Submodules Are Mind Blowing 🤯

Submodules allow you to include another repository as a sub-folder inside your git repo.

Git Submodules Are Mind Blowing 🤯
Do not index
Do not index
This is the first blog of many in a series where I chat about one new thing I learned or experienced.
Last week a team member at Sparc and I were talking about totally refactoring the notification services of our SAAS called Finergy. During this conversation, we realized that in developing this service we were likely to have a lot of redundancy between our admin and public API and wanted to create a separate notification API (for internal use) that combated this issue.
notion image
Photo by Christopher Gower on Unsplash
The problem is that across each of these services (Admin, Public, and Notifications), they needed all the same database configuration, models, migrations, etc. This meant any time we added or modified a model we needed to update all of our projects. But then we discovered Git Submodules and wow 🤯. We now have all of our database code maintained in one repository, and added as a submodule to every service that needs to interact with our database.

What are submodules?

Submodules allow you to include or embed one or more repositories as a sub-folder inside another repository. It’s that simple. In our case, we were able to reduce redundancy across multiple services by utilizing submodules.

Adding Submodules

Let’s say you are working on a project like ours. You have a repository for service and a repository database-module that contains all your database configuration code.
You can add database-module as a submodule to service by executing the following in the service repository.
git submodule add https://github.com/<org_or_username>/database-module.git database
It’s that simple, now the database folder will be available inside your service and you will be able to manage the source control for both repositories from your CLI or editor.
Note: With older versions of Git you will need to run git submodule update — init — recursive in order to download the content of the submodule into your local codebase
At this point, you can commit your changes in service and everything will be good to go. You can even see the submodule from the code tab on Github and Bitbucket.
notion image

Road Blocks

The largest roadblock we hit when implementing submodules into our services was updating our automatic deployment pipelines to handle the new submodule.
Our team mainly utilizes Google Cloud Run for these services, so if you also use Cloud Run and need some help with your deployment pipeline let me know!

Conclusion

Submodules are not always the best solution for issues like these, however, in our case, it works perfectly. Give it a try, and add this tool to your dev ops toolbelt. If you have any questions feel free to reach out!
 
Anthony Sette

Written by

Anthony Sette

I’m passionate about building technology that shapes our future on this planet. Currently building next-level software at my company, Sparc Agency.