【git命令】git submodule

git submodule

创建子模块:

# Usage
$ git submodule add [url] [path]

# With path
$ git submodule add https://github.com/laozhu/hugo-nuo themes/hugo-nuo
$ git submodule -b branch_name add https://github.com/laozhu/hugo-nuo themes/hugo-nuo # 指定分支

# Without path
$ cd themes
$ git submodule add https://github.com/laozhu/hugo-nuo
$ git submodule -b branch_name add https://github.com/laozhu/hugo-nuo # 指定分支

 

 

my-blog 这个仓库下面多出了一个 .gitmodules 文件,该文件列出了所包含的子模块列表,并为列表中每一个子模块指定了本地路径(path)和远程仓库地址(url),除此以外我们还可选为子模块指定 branch 分支,不指定默认为 master 分支。
$ cat .gitmodules
[submodule "themes/hugo-nuo"]
  path = themes/hugo-nuo
  url = https://github.com/laozhu/hugo-nuo

 

查看子模块:

$ git submodule
# 已检出子模块代码
cedbe91340dbcff661fa089b116441b11b050d38 themes/hugo-nuo (heads/master)
# 前面带 - 表示未检出代码,子模块是空文件夹
-cedbe91340dbcff661fa089b116441b11b050d38 themes/hugo-nuo (heads/master)

 

删除子模块:git中删除子模块略微麻烦一些,因为目前还没有 git submodule rm 这样的命令行,我们要做很多工作才能删得干净:

$ git submodule deinit themes/hugo-nuo # 子模块为themes/hugo-nuo
$ vim .gitmodules # 移除要删除的子模块
$ git add .gitmodules
$ git rm --cached themes/hugo-nuo
$ rm -rf .git/modules/themes/hugo-nuo
$ rm -rf themes/hugo-nuo
$ git commit -m "Remove submodule themes/hugo-nuo"

 

从远程仓库拉取子模块:

git submodule update --remote

 

参考资料

1. git submodule

2. git submodule的使用

3. 使用 Git Submodule 管理项目

4. Git submodule 知识总结

posted @ 2022-05-11 17:31  苏格拉底的落泪  阅读(37)  评论(0编辑  收藏  举报