[git] Git Submodule management project sub module


1. General

reference resources: https://www.cnblogs.com/nicksheng/p/6201711.html And modify.

2. Usage scenario

When the project becomes larger and larger, it is inevitable to split into multiple sub modules. We hope that each sub module has independent version management and is maintained by special people. At this time, we need to use the sub module function of git.

Common commands

git clone <repository> --recursive Clone the entire project recursively
git submodule add <repository> <path> Add sub module
git submodule init Initialization sub module
git submodule update Update sub module
git submodule foreach git pull Pull all sub modules

Adding a sub module is very simple. The command is as follows:

git submodule add <url> <path>

Where url is the path of the sub module and path is the directory path stored by the sub module.

After successful execution, git status will see that. gitmodules have been modified in the project and a new file (the path just added) has been added

git diff --cached view the modified content, you can see that the sub module is added, and the submission hash summary of the sub module is under the new file

git commit completes the addition of sub modules upon submission

3. How to use

3.1. Create a version library with modules

For example, we want to create a project with the following structure

project
  |--moduleA
  |--readme.txt

Create a project version library and submit the readme.txt file

git init --bare project.git
git clone project.git project1
cd project1
echo "This is a project." > readme.txt
git add .
git commit -m "add readme.txt"
git push origin master
cd ..

Create the moduleA version library and submit the a.txt file

git init --bare moduleA.git
git clone moduleA.git moduleA1
cd moduleA1
echo "This is a submodule." > a.txt
git add .
git commit -m "add a.txt"
git push origin master
cd ..

Introduce the sub module moduleA into the project and submit the sub module information

cd project1
git submodule add ../moduleA.git moduleA
git status
git diff
git add .
git commit -m "add submodule"
git push origin master
cd ..

You can see two more files to be submitted by using git status. gitmodules specifies the main information of the submodule, including the path and address information of the submodule. moduleA specifies the commit id of the submodule. You can see the contents of these two items by using git diff. It should be pointed out here that the GIT of the parent project does not record the file changes of the submodule. It specifies the git header of the submodule according to the commit id, so. gitmodules and module a need to be submitted to the remote warehouse of the parent project.

On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)
	new file:   .gitmodules
	new file:   moduleA

3.2. Clone the version Library of the tape module

Method 1: first clone the parent project, then initialize the submodule, and finally update the submodule. Initialization only needs to be done once, and then directly update each time. Note that the submodule is not on any branch by default, and it points to the submodule commit id stored in the parent project.

git clone project.git project2
cd project2
git submodule init
git submodule update
cd ..

Method 2: adopt the recursive parameter - recursive. Note that the submodule is not on any branch by default. It points to the submodule commit id stored in the parent project.

git clone project.git project3 --recursive

3.3. Modify sub module

After modifying a sub module, it will only affect the version Library of the sub module and will not affect the version Library of the parent project. If the parent project needs to use the latest sub module code, we need to update the submodule commit id in the parent project. By default, we can see that the submodule commit id in the parent project has changed by using git status, We just need to submit it again.

cd project1/moduleA
git branch
echo "This is a submodule." > b.txt
git add .
git commit -m "add b.txt"
git push origin master
cd ..
git status
git diff
git add .
git commit -m "update submodule add b.txt"
git push origin master
cd ..

3.4. Update sub module

When updating sub modules, it should be noted that the branch of sub modules is not master by default.

Method 1: first pull the parent project, and then execute git submodule update. Note that the branch of module a is never the master.

cd project2
git pull
git submodule update
cd ..

Method 2: first enter the sub module, then switch to the required branch, here is the master branch, and then pull the sub module. This method will change the branch of the sub module.

cd project3/moduleA
git checkout master
cd ..
git submodule foreach git pull
cd ..

3.5. Delete sub module

There are many ways to use this method on the Internet

git rm --cached moduleA
rm -rf moduleA
rm .gitmodules
vim .git/config

Delete submodule related content, such as the following

[submodule "moduleA"]
      url = /Users/nick/dev/nick-doc/testGitSubmodule/moduleA.git

Then submit to the remote server

git add .
git commit -m "remove submodule"

However, during my own local experiments, I found that the following methods can also be used. The server records. gitmodules and moduleA. As long as you delete moduleA locally with git's delete command, and then check the status with git status, you will find that both. gitmodules and moduleA have changed. As for. git/config, the submodule information will still be recorded, However, there is no impact on local use. If you clone from the server again, there will be no submodule information in. git/config.

git rm moduleA
git status
git commit -m "remove submodule"
git push origin master

4. Delete sub module

Sometimes the project maintenance address of the sub module changes, or the sub module needs to be replaced, the original sub module needs to be deleted.

Deleting a sub module is complex, and the steps are as follows:

  1. rm -rf sub module directory delete sub module directory and source code
  2. Vi. gitmodules delete the sub module related entries in the. Gitmodules file in the project directory
  3. Vi. git / config delete configuration item submodule related entries
  4. RM. Git / module / * delete the sub module directory under the module. Each sub module corresponds to a directory. Note that only the corresponding sub module directory can be deleted

After execution, execute the add sub module command. If an error is still reported, execute as follows:

git rm --cached Sub module name

After deletion, submit to the warehouse.

5. Our use

We have a project named ext XXX web, which is a repository dedicated to web interfaces

ext-xxx-web

Then we a back-end project as follows

seass
 seass-xxx  ours flink
 ext-web    ours web
   ext-provider
   ext-boot
   ext-front This is an empty directory, and then we want this to point to web

Then we finished it locally and pushed it up. However, when jenkince was packaged, it was executed first

git submodule update --init recursive --remote  ext-web/ext-front

After execution, there will be web related content in the EXT Web / ext front directory.

This article is the original article of the blogger's ninth senior brother (QQ:541711153, welcome to discuss technology). The blogger is not allowed to reprint it without permission.

Tags: git

Posted on Sat, 23 Oct 2021 21:00:39 -0400 by filn