Git Submodules and Their Applications in Sharing Shared Resources

Git Submodules and Their Applications in Sharing Shared Resources

Daily short news for you
  • Privacy Guides is a non-profit project aimed at providing users with insights into privacy rights, while also recommending best practices or tools to help reclaim privacy in the world of the Internet.

    There are many great articles here, and I will take the example of three concepts that are often confused or misrepresented: Privacy, Security, and Anonymity. While many people who oppose privacy argue that a person does not need privacy if they have 'nothing to hide.' 'This is a dangerous misconception, as it creates the impression that those who demand privacy must be deviant, criminal, or wrongdoers.' - Why Privacy Matters.

    » Read more
  • There is a wonderful place to learn, or if you're stuck in the thought that there's nothing left to learn, then the comments over at Hacker News are just for you.

    Y Combinator - the company behind Hacker News focuses on venture capital investments for startups in Silicon Valley, so it’s no surprise that there are many brilliant minds commenting here. But their casual discussions provide us with keywords that can open up many new insights.

    Don't believe it? Just scroll a bit, click on a post that matches your interests, check out the comments, and don’t forget to grab a cup of coffee next to you ☕️

    » Read more
  • Just got played by my buddy Turso. The server suddenly crashed, and checking the logs revealed a lot of errors:

    Operation was blocked LibsqlError: PROXY_ERROR: error executing a request on the primary

    Suspicious, I went to the Turso admin panel and saw the statistics showing that I had executed over 500 million write commands!? At that moment, I was like, "What the heck? Am I being DDoSed? But there's no way I could have written 500 million."

    Turso offers users free monthly limits of 1 billion read requests and 25 million write requests, yet I had written over 500 million. Does that seem unreasonable to everyone? 😆. But the server was down, and should I really spend money to get it back online? Roughly calculating, 500M would cost about $500.

    After that, I went to the Discord channel seeking help, and very quickly someone came in to assist me, and just a few minutes later they informed me that the error was on their side and had restored the service for me. Truly, in the midst of misfortune, there’s good fortune; what I love most about this service is the quick support like this 🙏

    » Read more

Issue

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.

What Is Git Submodule?

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.

git submodules work

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.

Applications in Sharing Shared Resources

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.

Conclusion

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:

Premium
Hello

5 profound lessons

Every product comes with stories. The success of others is an inspiration for many to follow. 5 lessons learned have changed me forever. How about you? Click now!

Every product comes with stories. The success of others is an inspiration for many to follow. 5 lessons learned have changed me forever. How about you? Click now!

View all

Subscribe to receive new article notifications

or
* The summary newsletter is sent every 1-2 weeks, cancel anytime.

Comments (2)

Leave a comment...
Avatar
Trịnh Cường1 year ago
quá muộn rồi bạn ạ, giờ làm cái mới thì dùng, chứ giờ src phình to quá ko refactor lại đc nữa
Reply
Avatar
Xuân Hoài Tống1 year ago
Vậy thì phải để dành cho dự án sau rồi ạ
Avatar
Trịnh Cường1 year ago
hay đấy bạn ơi, giờ mình mới biết. trước giờ toàn ```npm publish``` để làm src dùng chung
Reply
Avatar
Xuân Hoài Tống1 year ago
Giờ thì bạn có thể sử dụng git submodules để làm điều đó rồi :D