[2023]Git: Using Git Submodules for effective collaboration in Project development
Henrique Cardamone
Mid QA Engineer @ Sovos | SDET | QA Automation Engineer | Desktop/Web/Backend Testing | UI Automation | Automated Tests | Azure Pipelines | Robot Framework + Python
As a developer, you may often encounter situations where you need to use code from one project in another project. This can occur when you're developing a new project that requires functionality already present in an existing library, or when you're working on a shared library that needs to be integrated into multiple parent projects. However, using external code in your project can lead to a number of issues that need to be addressed.
One of the most common problems is that you want to treat the two projects as separate entities, yet still be able to use one from within the other. For instance, if you're developing a website that requires Atom feeds, you could use a pre-existing library to generate the feeds instead of creating the code from scratch. However, including the library code in your project as a shared library like a CPAN install or Ruby gem can make it difficult to customize the code and deploy it. Moreover, if you copy the source code of the library into your project tree, any custom changes you make could become difficult to merge when upstream changes become available.
Fortunately, Git provides a solution to this issue in the form of submodules. Git submodules allow you to keep a Git repository as a subdirectory of another Git repository, which means you can clone an external repository into your project and keep your commits separate. This approach allows you to use external code in your project without creating any conflicts or complexities in the development process.
By using Git submodules, you can avoid the pitfalls of copying code into your project or including it as a shared library. Instead, you can keep your code organized and maintainable while still benefiting from the functionality of external libraries. Git submodules provide a powerful tool for developers to manage their projects effectively and efficiently, and they are an essential part of any developer's toolkit.
Starting with Submodules
Git submodules allow you to integrate external Git repositories into your main project as subdirectories.
Here are projects that I'm using to explain, MAIN_PROJECT and PARENT_PROJECT.
2. Here is the PARENT_PROJECT that will receive the common folder by PROJECT_MAIN
To add a submodule, you use the "git submodule add" command and provide the URL of the repository you want to track (at the end, I'm provided the name of the folder that will receive the submodule). This lets you keep your projects organized and maintainable while still taking advantage of external libraries.
领英推荐
git submodule add -f https://[email protected]/your-organization/MAIN_PROJECT/_git/MAIN_REPO MAIN_REPO
If you run?git status?at this point, you’ll notice a few things.
Using Git submodules in this way allows us to keep our projects organized and maintainable, while still benefiting from the functionality of external libraries. With Git submodules, we can easily integrate multiple sub-projects into our main project, and keep track of all changes and updates in a straightforward manner.
Conclusion
We have learned how git submodules to integrate external Git repositories into main project as subdirectories. This helps to avoid issues when using external code in a project and allows for easy organization and maintenance. With Git submodules, can easily integrate multiple sub-projects into the main project while keeping track of all changes and updates in a straightforward manner. For more details, check the official git page here. Feel free for any questions. See you there.