The introduction to git submodule
文章目录
本文主要记录git submodule的相关知识。
官方spec:
man git-submodule- Git Tools - Submodules
本文内容主要转载自:Git submodule 子模块的管理和使用
1. 使用前提
经常碰到这种情况:当你在一个Git 项目上工作时,你需要在其中使用另外一个Git 项目。也许它是一个第三方开发的Git 库或者是你独立开发和并在多个父项目中使用的。这个情况下一个常见的问题产生了:你想将两个项目单独处理但是又需要在其中一个中使用另外一个。
在Git 中你可以用子模块submodule来管理这些项目,submodule允许你将一个Git 仓库当作另外一个Git 仓库的子目录。这允许你克隆另外一个仓库到你的项目中并且保持你的提交相对独立。
2. 添加子模块
此文中统一将远程项目https://github.com/maonx/vimwiki-assets.git克隆到本地assets文件夹。
1 | $ git submodule add https://github.com/maonx/vimwiki-assets.git assets |
添加子模块后运行git status, 可以看到目录有增加1个文件.gitmodules, 这个文件用来保存子模块的信息。
1 | $ git status |
3. 查看子模块
1 | $ git submodule |
4. 更新子模块
fetch all the data from that project and check out the appropriate commit listed in your superproject
1 | git submodule update |
更新子模块为远程项目的最新版本
1 | git submodule update --remote |
5. 克隆包含子模块的项目
克隆包含子模块的项目有二种方法:一种是先克隆父项目,再更新子模块;另一种是直接递归克隆整个项目。
5.1 克隆父项目,再更新子模块
1.克隆父项目
1 | $ git clone https://github.com/maonx/vimwiki-assets.git assets |
2.查看子模块
1 | $ git submodule |
子模块前面有一个-,说明子模块文件还未检入(空文件夹)。
3.初始化子模块
1 | $ git submodule init |
初始化模块只需在克隆父项目后运行一次。
4.更新子模块
1 | $ git submodule update |
5.2 递归克隆整个项目
1 | git clone https://github.com/maonx/vimwiki-assets.git assets --recursive |
递归克隆整个项目,子模块已经同时更新了,一步到位。
6. 删除子模块
7. MISC
常用操作:
git submodule update --init --recursive
解析:
git submodule initto initialize your local configuration filegit submodule updateto fetch all the data from that project and check out the appropriate commit listed in your superproject--recursivetraverse submodules recursively
参考资料: