Git Module / Submodule Automated Setups!
Git Submodule allows you to use other repositories as dependencies to your projects using only native git commands. It's a fantastic way to leverage git as a pseudo-package manager for quicker project workflows whilst completely decoupling technology dependency.
Git
Let's Git on with it!
Breaking larger code bases into a set of reusable modules is the hallmark of reusability, especially when applications have shared dependencies... Clearly you don't want to do the same thing over and over and over right? Isn't that basically the definition of insanity?
Well, fear not eager eyed reader, this is for you!
In this article I'm going to show how you can use tools built into Git to handle dependencies without any custom dependency management systems like composer, npm, yarn, bla bla bla! (Full-disclosure: you'll still be using those, but at least you don't have to bake stuff in to your codebase)
The Anatomy of a Submodule File!
File Name: .gitmodules
Function: To house all values required to determine submodules within a Git Repository.
Adding New Modules
To add a new submodule, run the command;
$ git submodule add <RepositoryURL> <VendorPath>/<SubmodulePath>
Installing / Updating Submodules
To install your submodules for the first time, you will need to ensure that the destination path within your codebase exists.
In the case of the sample module file, we would need to create the vendor
directory.
$ mkdir vendor
Hot Tip: Make sure you set your .gitignore
file to ignore your vendor / third-party dependency directory, or you'll commit third party code to your Repo. Very messy stuff.
Installing using 'Git Submodule' Commands.
Note: This might not always be available on your system.
- Init our submodules;
$ git submodule init
- Update our submodules;
$ git submodule update
Installing using Bash
Note: We recommend using the bash file below for this purpose.
Using the file above, we run;
-
Set the execute permission to the bash file
$ chmod +x install_submodules.sh
-
Execute the bash file.
$ ./install_submodules.sh
One-Stop Submodule Installation Shop
Let's put it all together for convenience purposes okay?
-
Set the execute permission on the
install.sh
file.
$ chmod +x install.sh
-
Execute the
install.sh
file.
$ ./install.sh
Conclusion
Submodules are a fantastic way to split up a monolithic codebase into smaller codebases for better modularity, dependency resolution, and code de-duplication.
It's especially helpful when it's infeasible to use a package management system like NPM, Yarn, PIP, dotnet nuget, or composer.
We recommend you familiarise yourself with the Git SCM Documentation on Submodules, even though reading documentation can be very boring. https://git-scm.com/book/en/v2/Git-Tools-Submodules