Mono & Msa : Front 6. Git Submodules


Posted on Fri, Dec 9, 2022 architecture git microservice

Git Submodules and Why it Matters

Git submodules are a way of embedding a repository in your parent repository. We've been talking about decoupling and promoting loose-system design throughout this article.

In the same way, git submodules also promote technical advantages to decouple your system with version control technologies by splitting your monorepos packages into their own repositories with their own pipelines.

Git submodules help us to set up a development environment where monorepos can share modules with other monorepos. That’s just like building a nested repo. Shared-module participates in more than one mono repository with a git submodule.

The folder structure of a git submodule with monorepo looks like the one below :

/mono-repository1
  /app1
  /app2
  /shared-module-as-a-git-submodule
/mono-repository2
  /app3
  /shared-module-as-a-git-submodule

From the above structure, you can see that two monorepos are sharing a shared-module-as-a-git-submodule.

🀷 Why use git submodules?

Most of us are comfortable with our conventional git setup. Why then should you bother about git submodules? There are many benefits that git submodules bring to your version control game. Below are some of them.

  1. Separate code into different repositories.

    If you're working on a project whose codebase has big components, you can convert those components to git submodules. You'll maintain a cleaner git log by doing so.

  2. Submodules can be added to many repositories.

    This is of huge importance if you have many components that you want to share. You can easily update the submodule in all the repositories that added them. This will save you a whole lot of time.

Basic Git Submodule Operations

Getting things done with a submodule is quite straightforward like normal git. Below are some basic operations you can carry out with the git submodule.

Adding a submodule

Adding submodules to a repository is very simple. Follow the steps below.

git submodule adds submodule_name.git to_main.git

This will checkout the code of submodule_name.git to main.git and also add details about the submodule in the directory.

Getting the submodule's code

If you want to access some code in a submodule in a repository.

git submodule init

This will pull all the codes in our submodule and place them in a directory it is configured to be.

Committing changes inside a submodule

Committing changes to a submodule is just like working with a normal git commit. Follow the steps below :

  1. cd into your submodule
  2. Make desired changes
  3. git commit changes
  4. git push changes
  5. cd back to the main repository
  6. Use git status to see the modified submodule directory.
  7. Run git diff to see before and after change differences.
  8. Commit in the main directory to update it.

Iterating through many git submodule

We can loop over many submodule file with the command below :

git submodule foreach npm install

The command above installs all dependencies needed by our applications inside a submodule.

Git pull and clone in a submodule

This is quite different from the git commit and git clone that you know. You can pull and clone a submodule through the following procedures.

Update your submodule by running :

git submodule update

add the --init flag if it has not been initiated. You can also add the --recursive flag to initiate and update submodules and make the whole process easier with the codes below :

git pull --recursive -submodule

This will pull the main repository together with its submodules :

git clone --recursive -submodule

This will clone a parent directory together with its submodules.

Conclusion

Decoupled systems have many advantages over coupled systems. This is simply because of the rise in the need for collaboration between teams, and the need for an increase in performance with minimal system configurations.

The monolithic architecture may seem to be obsolete, but it still has some practical application in this data-driven age. For instance, a monolithic architecture is an ideal setup if you're building an inelastic app that requires small maintenance and no scaling.

Knowing when to choose any of the system architectures and approaches in this article will go a long way toward improving your overall product and team performance.

Thanks for staying to this point! πŸš€