Git子模块

本文介绍如何使用Git的子模块功能。

背景

在建立博客时,我使用了两个仓库,分别是博客内容tech和博客模板hexo-theme-pure。在tech中,需要将hexo-theme-pure作为子模块使用。

在已有仓库中添加子模块

假如我们已经初始化了tech仓库,这时需要在./themes/pure中添加子模块(另一个仓库)。这时需要执行下面的命令

1
git submodule add https://github.com/DoctorKey/hexo-theme-pure ./themes/pure

执行成功后,hexo-theme-pure会拉取到/themes/pure文件夹中。

克隆含有子模块的仓库

若一个仓库含有子模块,我们可以用recursive的克隆方式,将该仓库及其子模块一起克隆。

1
git clone --recursive https://github.com/DoctorKey/tech.git

若上述命令执行失败,可使用下面的方法,单独更新子模块。

拉取最新版本

更新项目内子模块到最新版本。

1
git submodule update

更新子模块为远程项目的最新版本

1
git submodule update --remote

查看子模块

1
git submodule

推送子模块

1
2
3
4
5
cd /themes/pure
git status
git add .
git commit
git push