Git Submodule management project sub module

Keywords: git

Git Submodule management project sub module

 

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

How to use

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

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 same 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 project3 --recursive

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 ..

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 ..

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

Posted by rockindano30 on Tue, 30 Nov 2021 03:20:49 -0800