Git Submodule manages project submodules

scenes to be used

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

Common commands

git clone <repository> --recursive 递归的方式克隆整个项目
git submodule add <repository> <path> 添加子模块 git submodule init 初始化子模块 git submodule update 更新子模块 git submodule foreach git pull 拉取所有子模块

how to use

1. Create a repository with submodules

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

project
  |--moduleA
  |--readme.txt

Create a project repository 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 repository 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 submodule moduleA into the project project and submit the submodule 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 ..

Using git statusit, you can see two more files that need to be submitted. .gitmodulesThe main information of the submodule is specified, including the path and address information of moduleAthe submodule, and the commit id of the submodule is specified. git diffYou can see the contents of these two items. It needs to be pointed out 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 .gitmodulesthese moduleAtwo items 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 repository with submodules

Method 1, first clone the parent project, then initialize the submodule, and finally update the submodule. The initialization only needs to be done once, and then you only need to update directly each time. It should be noted that the submodule is not on any branch by default, it points to the storage of the parent project. submodule commit id.

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

Method 2, using recursive parameters --recursive, it should be noted that the same 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. Modify the submodule

After modifying the submodule, it only affects the repository of the submodule, and will not have any effect on the repository of the parent project. If the parent project needs to use the latest submodule code, we need to update the submodule commit id in the parent project. The default is We git statuscan see that the submodule commit id in the parent project has changed, 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 submodules

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

Method 1: Pull the parent project first, and then execute git submodule updateit. Note that the branch of moduleA is not always master.

cd project2
git pull
git submodule update
cd ..

Method 2, first enter the submodule, then switch to the desired branch, here is the master branch, and then pull the submodule, this method will change the branch of the submodule.

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

5. Remove submodules

There are many ways to use the following 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, when I experimented locally, I found that the following method is also possible. The server records the .gitmodulessum moduleA, as long as I use the git delete command to delete moduleA, and then use git status to check the status, I will find that both .gitmodules and moduleA have been Changed. As for .git/config, submodule information will still be recorded, but local use has not been found to have any effect. If re-cloning from the server, there will be no submodule information in .git/config.

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

scenes to be used

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

Common commands

git clone <repository> --recursive 递归的方式克隆整个项目
git submodule add <repository> <path> 添加子模块 git submodule init 初始化子模块 git submodule update 更新子模块 git submodule foreach git pull 拉取所有子模块

how to use

1. Create a repository with submodules

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

project
  |--moduleA
  |--readme.txt

Create a project repository 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 repository 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 submodule moduleA into the project project and submit the submodule 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 ..

Using git statusit, you can see two more files that need to be submitted. .gitmodulesThe main information of the submodule is specified, including the path and address information of moduleAthe submodule, and the commit id of the submodule is specified. git diffYou can see the contents of these two items. It needs to be pointed out 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 .gitmodulesthese moduleAtwo items 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 repository with submodules

Method 1, first clone the parent project, then initialize the submodule, and finally update the submodule. The initialization only needs to be done once, and then you only need to update directly each time. It should be noted that the submodule is not on any branch by default, it points to the storage of the parent project. submodule commit id.

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

Method 2, using recursive parameters --recursive, it should be noted that the same 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. Modify the submodule

After modifying the submodule, it only affects the repository of the submodule, and will not have any effect on the repository of the parent project. If the parent project needs to use the latest submodule code, we need to update the submodule commit id in the parent project. The default is We git statuscan see that the submodule commit id in the parent project has changed, 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 submodules

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

Method 1: Pull the parent project first, and then execute git submodule updateit. Note that the branch of moduleA is not always master.

cd project2
git pull
git submodule update
cd ..

Method 2, first enter the submodule, then switch to the desired branch, here is the master branch, and then pull the submodule, this method will change the branch of the submodule.

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

5. Remove submodules

There are many ways to use the following 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, when I experimented locally, I found that the following method is also possible. The server records the .gitmodulessum moduleA, as long as I use the git delete command to delete moduleA, and then use git status to check the status, I will find that both .gitmodules and moduleA have been Changed. As for .git/config, submodule information will still be recorded, but local use has not been found to have any effect. If re-cloning from the server, there will be no submodule information in .git/config.

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325156087&siteId=291194637