Integrated or created package managers used in programming languages are an effective way for us to reuse shared code. For example, in JavaScript/Node.js, there is npm with millions of packages shared by many programmers worldwide. Whenever we need something, what we usually do first is to look for a package that meets our needs to reduce software development time.
Sharing packages that anyone can search for and use may not always be suitable in some cases. For example, we create utility functions only for use in our company's internal projects or for personal use without sharing with anyone else. Although many package managers provide features to "publish" packages for internal use, there are still certain limitations such as costs and deployment time...
In git, there is a feature called submodule that can help us solve cases where we need to share folders or files with each other in repositories, so we don't have to spend a lot of effort to share utility code between projects. The details of how to do this are explained in the article below.
Git Submodule is a powerful feature in Git that allows you to add and manage other repositories within your repository. It provides a way to easily integrate and track external source code dependencies.
Basically, a submodule is a Git repository embedded in another Git repository. It acts as a pointer to a specific commit in an external repository. By using submodules, you can add and use external source code libraries, frameworks, or any other projects as part of your project.
The simplest way to understand how submodules work is to share common code in different projects. For example, you create a repository that contains only files with utility functions (utils), and then you want to use them in another project. You can simply create a directory linked to the utility repository.
Utilizing submodules provides us with many conveniences in collaboration and code reuse because submodules are also Git repositories, so they can be further developed with features, bug fixes, maintenance, etc., like regular projects. When you want to receive changes, you only need to pull
the submodule to get the latest code. You can even participate in developing submodules directly within the project linked to them by accessing the submodule directory and committing as usual.
Suppose we are in project directory A and want to add submodule B with URL: https://github.com/2coffee/awesomelibrary.
Add a submodule using the add
command:
$ git submodule add https://github.com/2coffee/awesomelibrary
Check what the command just did:
$ git status
On branch main
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: .gitmodules
new file: awesomelibrary
Git reports that 2 new files have been added, which contain references to the linked submodule.
Try opening the .gitmodules
file, and you will see the content looks like this:
[submodule "awesomelibrary"]
path = awesomelibrary
url = https://github.com/2coffee/awesomelibrary
Now you can check the awesomelibrary
directory and use the code in it as if it belongs to the project.
In addition to copying or packaging code for use in another internal project, we also have another way to use Git submodules. It's simply linking another Git project to a directory in the current project, so we can easily receive the latest changes from it without having to go through many steps.
References:
The secret stack of Blog
As a developer, are you curious about the technology secrets or the technical debts of this blog? All secrets will be revealed in the article below. What are you waiting for, click now!
Subscribe to receive new article notifications
Hello, my name is Hoai - a developer who tells stories through writing ✍️ and creating products 🚀. With many years of programming experience, I have contributed to various products that bring value to users at my workplace as well as to myself. My hobbies include reading, writing, and researching... I created this blog with the mission of delivering quality articles to the readers of 2coffee.dev.Follow me through these channels LinkedIn, Facebook, Instagram, Telegram.
Comments (2)