Git supports adding other projects as submodules in the project. This article will explain the usage in detail
Table of contents
1. Add submodules to the Git project
The submodule mentioned here is actually another project managed by Git. Assuming that there is already a project named subsampleproject, use it as a submodule of sampleproject.
1. First clone the main project locally
Clone the sampleproject project to the local
sun@sun-pc:~/myProjects/codechina$ git clone [email protected]:In_engineer/sampleproject.git
2. Use the git command to add submodules
Enter the sampleproject directory and add the subsampleproject to the 3rd_party of the sampleproject. Note that the 3rd_party here does not need to be created manually. After this step is executed, you can see that 3rd_party already contains the content of subsampleproject.
sun@sun-pc:~/myProjects/codechina/sampleproject$ git submodule add [email protected]:In_engineer/subsampleproject.git 3rd_party
正克隆到 '/home/sun/codechina/sampleproject/3rd_party'...
remote: Enumerating objects: 73, done.
remote: Total 73 (delta 0), reused 0 (delta 0), pack-reused 73
接收对象中: 100% (73/73), 128.83 KiB | 2.01 MiB/s, 完成.
处理 delta 中: 100% (1/1), 完成.
3. Directory changes after adding submodules
① There are more submodules in the project
② There is one more .gitmodule
hidden file
③ .git/config
Changes
4. Commit the changes to the remote repository
It is not needed here git add
, because git submodule
the changes will be automatically updated to the staging area.
The changes in the remote warehouse are as follows:
2. Clone the project containing the submodules
1. Clone the project
sun@ubuntu:~/myProject$ git clone [email protected]:In_engineer/sampleproject.git
2. Synchronize the url information of the submodule
sun@ubuntu:~/myProject/sampleproject$ git submodule sync
为 '3rd_party' 同步子模组 url
...
3. Initialize the local configuration file
If the second step is not done, an error may be reported: the submodule '3rd_party' ([email protected]:In_engineer/subsampleproject.git) is not registered for the path '3rd_party'
sun@ubuntu:~/myProject/sampleproject$ git submodule init
4. Update the submodule to the local
sun@ubuntu:~/myProject/sampleproject$ git submodule update
正克隆到 '3rd_party'...
remote: Enumerating objects: 73, done.
remote: Total 73 (delta 0), reused 0 (delta 0), pack-reused 73
接收对象中: 100% (73/73), 128.83 KiB | 0 bytes/s, 完成.
处理 delta 中: 100% (1/1), 完成.
检查连接... 完成。
子模组路径 '3rd_party':检出 '8c63ff33ccb67b51078ddf09f3054cb068b680aa'
3. Modify the content in the submodule and upload it to its remote warehouse
The submodule is also managed by a separate warehouse, so the general steps are the same as the normal submission. You can refer to: Git series explanation - multi-person collaborative development under the same branch .
It should be noted here that the submodule warehouse cloned above is not in a specific For the branch, you need to create a local branch first, and then merge to the master branch for submission when submitting
sun@ubuntu:~/myProject/sampleproject/3rd_party$ git branch dev
sun@ubuntu:~/myProject/sampleproject/3rd_party$ git checkout dev
sun@ubuntu:~/myProject/sampleproject/3rd_party$ touch test.c
sun@ubuntu:~/myProject/sampleproject/3rd_party$ git add test.c
sun@ubuntu:~/myProject/sampleproject/3rd_party$ git commit -m "add test.c"
sun@ubuntu:~/myProject/sampleproject/3rd_party$ git checkout master
sun@ubuntu:~/myProject/sampleproject/3rd_party$ git push origin master