Git子模块的应用

一、子模块的介绍

git子模块介绍
有一个需求,就是想把项目中的代码和文档分开,并且都添加权限。即有些人有下载项目文档的权限,有些人只有代码相关的权限。这个gitlab是没有这个功能的,但是可以通过git的子模块的方式实现。

二、子模块的创建

2.1、带子模块的版本库目录结构

首先,我们完成后的项目目录大概如下图所示。test-project-doc为项目的文档,然后test.txt可以当成是代码。
在这里插入图片描述

2.2、创建test-project版本库

git init 和 git --bare init 的区别在于:
git init 带工作区
git --bare init 是不带工作区的,只有版本库,想要看git里面的内容只能通过git clone address的方式克隆出来

git init --bare test-project.git
git clone test-project.git test-project
cd test-project
echo "This is a project." > text.txt
git add .
git commit -m "add text.txt"
git push origin master

2.3、创建test-project-doc文档版本库

git init --bare test-project-doc.git
git clone test-project-doc.git test-project-doc
cd test-project-doc
echo "This is a submodule test-project-doc." > a.txt
git add .
git commit -m "add a.txt"
git push origin master

2.4、将文档库引入到项目版本库中

cd test-project
git submodule add ../test-project-doc.git test-project-doc
git status
git diff
git add .
git commit -m "add submodule test-project-doc"
git push origin master

三、带子模块项目的拉取

3.1、克隆父项目test-project

接下来我们将会克隆一个含有子模块的项目。 当你在克隆这样的项目时,默认会包含该子模块目录,但test-project-doc中还没有任何文件:

git clone http://192.168.2.99:8089/domestic-group/test-project.git
cd test-project

你必须运行两个命令:git submodule init 用来初始化本地配置文件,而 git submodule update 则从该项目中抓取所有数据并检出父项目中列出的合适的提交。

git submodule init
git submodule update

3.2、简便方法clone带子项目的库

不过还有更简单一点的方式。 如果给 git clone 命令传递 --recursive 选项,它就会自动初始化并更新仓库中的每一个子模块。

git clone --recursive http://192.168.2.99:8089/domestic-group/test-project.git

四、在包含子模块的项目上工作

4.1、在test-project-doc文档库中添加一个b.txt文件并上传到远程库中

在test-project-doc文件夹下,创建一个b.txt文件。内容为This is b.
在这里插入图片描述
提交文件到远程仓库
在这里插入图片描述
在Gitlab上已经可以看到我们提交的这个b.txt文件了。
在这里插入图片描述

4.2、在另一台设备中更新代码,下载b.txt文件。

如下图所示,一开始这台设备上的test-project-doc文件夹下只有a.txt文件。然后执行了更新命令后,可以看到我们已经拉取到了b.txt文件。
在这里插入图片描述

git submodule update --remote
发布了76 篇原创文章 · 获赞 16 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_38657051/article/details/103307832
今日推荐