Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use submodules and subtrees to manage Git projects

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you about how to use sub-modules and sub-trees to manage Git projects. The article is rich in content and analyzed and described from a professional point of view. I hope you can get something after reading this article.

If you are involved in the development of an open source project, you are probably already using Git to manage your source code. You may have come across projects with a lot of dependencies and / or subprojects. How do you manage them? For an open source organization, it is tricky to implement single-source documentation and dependency management for communities and products. Documents and projects tend to be fragmented and redundant, making them difficult to maintain.

Suppose you want to treat a single project as a child in a repository, the traditional method is to copy the project to the parent repository, but what if you want to use the same child project in multiple parent projects? If you copy child projects to all parent projects, it is not feasible for you to make changes in each parent project when there are updates. This can lead to redundancy and data inconsistencies in the parent project, making it difficult to update and maintain child projects.

imperative

Git submodules and subtrees

What if you could put one project into another with one command? What if you can add a project as a subproject to any number of projects at any time and update changes synchronously? Git provides solutions to these problems: Git submodule submodule and Git subtree. These tools were created to support the development workflow of shared code at a more modular level, and to bridge the gap between the Git repository source code management source-code management (SCM) and the subtrees below it.

The following is a real application scenario of the concepts to be described in detail in this article. If you are already familiar with the tree structure, the model looks like this:

What is the Git submodule?

Git provides submodules in its default package, which can embed Git repositories into other repositories. Specifically, the Git submodule points to a commit in the subtree. Here's what the Git submodule in my Docs-test GitHub repository looks like:

The folder @ submit Id format indicates that the repository is a submodule, and you can click on the folder directly to enter the subtree. The configuration file named .gitmodules contains details of all sub-module repositories. The .gitmodules file for my repository is as follows:

You can use the Git submodule in your repository with the following command:

Clone a repository and load submodules

Clone a repository that contains submodules:

$git clone-recursive

If you have cloned the repository before, you want to load its submodules now:

$git submodule update-init

If there are nested submodules:

$git submodule update-init-recursive

Download sub-module

Downloading multiple submodules sequentially is a tedious task, so clone and submodule update support the-jobs (or-j) parameter:

For example, to download 8 submodules at a time, use:

$git submodule update-init-recursive-j 8$ git clone-recursive-jobs 8

Pull sub-module

Before running or building the parent project, you need to make sure that the dependent child projects are up-to-date.

Pull all modifications to the sub-module:

$git submodule update-remote

Use sub-modules to create the repository:

Add a subtree to a parent repository:

$git submodule add

Initialize an existing Git submodule:

$git submodule init

You can also create branches and trace submissions in the sub-module by adding the-update parameter to the submodule update command:

$git submodule update-remote

Update submodule submission

As mentioned above, a submodule is a link to a submission in the subtree. If you want to update the submodule submission, don't worry. You do not need to explicitly specify the latest submission. You only need to use the generic submodule update command

$git submodule update

Just as you would normally create a parent repository and push it to GitHub, just add and submit it.

Remove a child module from a parent repository

Simply manually deleting a child project folder does not remove the child project from the parent project. To delete a submodule named childmodule, use:

$git rm-f childmodule

Although the Git submodule looks easy to use, there are certain barriers for beginners to use.

What is a Git subtree?

The Git subtree, introduced in Git 1.7.11, allows you to embed a copy of any repository as a subdirectory in another repository. It is one of several ways Git projects can inject and manage project dependencies. It holds external dependency information in regular submissions. Git subtrees provide neat integration points, so it's easy to recover them.

If you refer to the subtree tutorial provided by GitHub to use a subtree, you will not see the .gittree configuration file locally whenever you add a subtree. This makes it difficult to tell which subtree is, because they look like ordinary folders, but they are copies of the subtree. The default Git package does not provide a Git subtree version with a .gittree configuration file, so if you want a git-subtree command with a .gitbread configuration file, you must download git-subtree from the / contrib/subtree folder of the Git source repository.

You can clone any repository that contains a subtree like any other regular repository, but because there is a copy of the entire subtree in the parent repository, the cloning process may take a long time.

You can use the Git subtree in your repository with the following command.

Add a subtree to the parent repository

To add a subtree to the parent repository, you first need to execute remote add, followed by the subtree add command:

$git remote add remote-name$ git subtree add-prefix=folder/ remote-namesubtree-branchnam

The above command merges the submission history of the entire child project into the parent repository.

Push and pull changes to and from the subtree

$git subtree push-all

Or

$git subtree pull-all

Which one should you use?

Any tool has its advantages and disadvantages. Here are some features that may help you decide which is best for you:

The repositories of the Git submodules take up less space because they are just links to a commit of the subproject, while the Git subtree holds the entire subproject and its submission history.

The Git submodule needs to be accessible in the server, but the subtree is decentralized.

Git submodules are widely used in component-based development, while Git subtrees are mostly used in system-based development.

The Git subtree is not a direct alternative to the Git submodule. There are clear instructions on which one to use. If you have an external repository that belongs to you and the usage scenario is to push the code back to it, use the Git submodule because it is easier to push the code. If you have third-party code and don't push code to it, use the Git subtree because it's easier to pull the substitution code.

The above is the editor for you to share how to use sub-modules and sub-trees to manage Git projects, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report