In your project, you may encounter scenarios where you add other Git repositories to one Git repository. For example, reference third-party libraries in your project. Or in modular development, where some common modules need to be maintained separately, it is convenient to use a separate repository, but need to be referenced in the project. Here you use Git’s Git submodule command to add child Git projects to a Git project.
You can use git submodule –help to see all the related commands. For the convenience of explanation, two sub-modules liba and libb are added to the MainProject MainProject.
1. Add submodules
Enter MainProject and use git submodule add to add.
cd MainProject/
git submodule add https://github.com/imtianx/liba.git
Copy the code
The diagram below:
Use git submodule add https://github.com/imtianx/libb.git add libb module. For the above figure, folder liba is the newly added submodule directory,.gitModules stores the information of the submodule, use cat or vim to check the content:
[submodule "liba"]
path = liba
url = https://github.com/imtianx/liba.git
[submodule "libb"]
path = libb
url = https://github.com/imtianx/libb.git
Copy the code
Gitmodules file: Saves the mapping between the project URL and the local directory that has been pulled. Multiple submodules contain multiple records that will be pulled and pushed along with version control.
The file directory tree is as follows:
. ├ ─ ─ the README. Md ├ ─ ─ liba │ ├ ─ ─ the README. Md │ ├ ─ ─ a.t xt │ └ ─ ─ a2. TXT ├ ─ ─ libb │ ├ ─ ─ the README. Md │ ├ ─ ─ b.t xt │ └ ─ ─ b2. TXT └ ─ ─ test.textCopy the code
Finally, commit the added submodule to the home directory
$ git commit -m "add liba and libb submodules"
[master 6b15e30] add liba and libb submodules
3 files changed, 8 insertions(+)
create mode 100644 .gitmodules
create mode 160000 liba
create mode 160000 libb
Copy the code
2. Update submodules
Often submodules are developed separately, here is an example of updating liba (for testing purposes, a file is added in the liBA repository first) :
cd liba/
git fetch
git merge origin/master
Copy the code
The following figure shows the operation result. Note that you need to enter the submodule directory:
Alternatively, you can update the libb submodule directly from your home directory with the following command:
git submodule update --remote liba
Copy the code
Update using the following methodlibb
的 dev
Branches:
git config -f .gitmodules submodule.liba.branch dev
git submodule update --remote
Copy the code
The diagram below:
-f to.gitModules, which is valid for all users after modification.
3. Delete submodules
In daily development, there is a need to add and, of course, remove submodules. Here, the main project contains two sub-modules: liba and libb. Take deleting liba as an example:
- use
git rm --cached liba
Delete liba from version control (local version still exists), if not necessary--cached
Delete it completely. - use
vim .gitmodules
Open Vim to edit and delete the corresponding content
[submodule "liba"]
path = liba
url = https://github.com/imtianx/liba.git
branch = dev
Copy the code
- use
vim .git/config
Open Vim to edit and delete the corresponding content
[submodule "liba"]
url = https://github.com/imtianx/liba.git
active = true
Copy the code
- use
rm -rf .git/modules/liba
, delete the cache module under.git, and finally submit the project.
Submodules can also be added after the above deletion.
4. Clone the warehouse with sub-modules
If you want to clone a repository containing submodules that cannot be pulled directly, add the –recursive parameter as follows:
git clone --recursive https://github.com/imtianx/MainProject.git
Copy the code
Or use the following three steps:
git clone https://github.com/imtianx/MainProject.git
git submodule init
git submodule update
Copy the code
For more operations on submodules, please refer to the official documentation: Git Tools – Submodules