1. 子模块的添加

git submodule add <url> <path>

其中,url为子模块的路径,path为该子模块存储的目录路径。

比如:

git submodule add https://github.com/openresty/echo-nginx-module.git external/echo-nginx-module


执行成功后,git status会看到项目中修改了.gitmodules,并增加了一个新文件(为刚刚添加的路径)

root@dispatch:/home/Xlon# git status
On branch feature_ai_20190712
Your branch is up-to-date with 'origin/feature_ai_20190712'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

modified: .gitmodules
new file: external/echo-nginx-module

git diff --cached查看修改内容可以看到增加了子模块,并且新文件下为子模块的提交hash摘要

git commit提交即完成子模块的添加

git push 推送到仓库就ok了


2. submodule 的使用

git submodule init

git submodule update


3. ​​.gitmodules 和.git/config​

Git在两个地方存储有关子模块的信息。第一个是在一个名为的文件中​​.gitmodules​​,该文件被签入git存储库。对此文件的更改将传播到其他存储库。

另一个位置在​​.git/config​​。

当更新存储库时,您​​.gitmodules​​​将更新文件的副本,但不会更新您的​​.git/config​​​文件; 当想要从新网址开始签出时,执行​​git submodule sync​​​以将新配置从中复制​​.gitmodules​​​到​​.git/config​​。



重点:

删除Submodule

​git​​​ 并不支持直接删除​​Submodule​​需要手动删除对应的文件:


cd pod-project

git rm --cached pod-library
rm -rf pod-library
rm .gitmodules

更改git的配置文件​​config​​:
vim .git/config


可以看到​​Submodule​​的配置信息:

[submodule "pod-library"]
url = git@github.com:jjz/pod-library.git

删除submodule相关的内容,然后提交到远程服务器:


git commit -a -m 'remove pod-library submodule'

git push branch  , 上传到远程分支