「Git」- submodule,引用其他仓库

git-submodule – Initialize, update or inspect submodules

添加子模块(增)

git – How do I add a submodule to a sub-directory? – Stack Overflow
How can I specify a branch/tag when adding a Git submodule? – Stack Overflow

# 添加子模块
git submodule add "<git@github ...>"

# 添加子模块到子目录
git submodule add "<repo>" "path/to/folder"

# 注意事项,-b 选项不支持指定 Tag,否则会提示:
# git submodule fatal: 'origin/xxx' is not a commit and a branch 'xxx' cannot be created from it
git submodule add -b "<branch>" "<repo>" "path/to/folder"

#######################################
## 如果需要添加特定分支,可以使用如下命令:
#######################################
git submodule add "<your repo>" path/to/folder
cd path/to/folder
git checkout v1.0.2
cd -
git add path/to/folder
git commit -m "moved submodule to v1.0.2"
git push

删除子模块(删)

How effectively delete a git submodule.

从仓库中删除子模块,有很多不同的方法,似乎没有统一的做法

或:

mv path/to/module path/to/module.backup

git submoduel deinit --force -- path/to/module
rm -rf .git/modules/path/to/module
git rm --force path/to/module
# Note: path/to/module (no trailing slash)

或:

git submodule deinit "<path_to_submodule>"
git rm -f "<path_to_submodule>"
git commit -m "Removed submodule"
rm -rf .git/modules/<submodule>

更新子模块(改)

在 git clone 后,执行如下命令,来检出当前仓库的子模块:

git submodule update --init

git submodule update --init --recursive                     # 递归拉取子模块

参考文献

man 1 git-submodule, Git 2.20.1
Git submodule and git clone create names in lowercase – Stack Overflow
How effectively delete a git submodule.
git – How do I add a submodule to a sub-directory? – Stack Overflow
Using submodules in Git – Tutorial