Git Submodule Managing Project Sub-modules

Source: Internet
Author: User

Usage Scenarios

When the project is more and more large, inevitably split into multiple sub-modules, we hope that each sub-module has a separate version management, and by dedicated people to maintain, this time we need to use Git submodule function.

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 for the tape module

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

project  |--moduleA  |--readme.txt

Create a project repository and submit a Readme.txt file

clone project.git project1cd project1echo "This is a project." > readme.txtgit add .git commit -m "add readme.txt"git push origin mastercd ..

Create the Modulea repository and submit the A.txt file

clone moduleA.git moduleA1cd moduleA1echo "This is a submodule." > a.txtgit add .git commit -m "add a.txt"git push origin mastercd ..

Introduce Submodule Modulea in Project projects and commit submodule information

commit -m "add submodule"git push origin mastercd ..

git statusyou can see more than two files that need to be submitted, which .gitmodules specify the primary information for submodule, including the path and address information for the submodule, moduleA specify the commit ID of the submodule, and use git diff the content that you can see for both items. It is necessary to point out that the parent project's git does not log submodule file changes, it specifies the git header of submodule according to the commit ID, so the .gitmodules moduleA two items are required to be submitted to the remote repository of the parent project.

On branch masterYour 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 One, first clone the parent project, then initialize the submodule, and finally update the submodule, initialization only need to do once, then each time only need to update directly, you need to note that submodule default is not on any branch, It points to the Submodule commit ID of the parent project store.

clone project.git project2cd project2git submodule initgit submodule updatecd ..

Method two, with recursive parameters --recursive , note that the same submodule default is not on any branch, it points to the Submodule commit ID of the parent item store.

git clone project.git project3 --recursive
3. Modifying the Sub-module

Modifying a submodule only affects the repository of the child module, has no effect on the parent project's repository, and if the parent project requires the latest Submodule code, we need to update the Submodule commit ID in the parent project, by default we use git status You can see that the Submodule commit ID has changed in the parent project, and we just need to submit it again.

cd project1/moduleAgit branchecho "This is a submodule." > b.txtgit add .git commit -m "add b.txt"git push origin mastercd ..git statusgit diffgit add .git commit -m "update submodule add b.txt"git push origin mastercd ..
4. Updating submodules

When updating submodules, be aware that the sub-module branches are not master by default.

Method One, pull the parent project first, then execute git submodule update , noting that Modulea's branch is not always master.

cd project2git pullgit submodule updatecd ..

Method Two, first enter the submodule, then switch to the desired branch, here is the Master branch, and then the sub-module pull, this method will change the branch of the Submodule.

cd project3/moduleAgit checkout mastercd ..git submodule foreach git pullcd ..
5. Deleting a sub-module

There are a lot of ways to use the Internet.

--cached moduleArm -rf moduleArm .gitmodulesvim .git/config

Delete Submodule related content, such as the following

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

Then commit to the remote server

commit -m "remove submodule"

But my own local experiment, I found the following way can also, the server record is .gitmodules and moduleA , as long as the git delete command to delete Modulea, and then use Git Status check state will be found. Gitmodules and Modulea both have changed, and as for. Git/config, submodule information is still logged, but local use does not find any effect if cloning from the server again. git/ There is no submodule information in CONFIG.

commit -m "remove submodule"git push origin master

Git Submodule Managing Project Sub-modules

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.