git submodule

1. $ git submodule add 远程地址 [本地Path]
拉取某个地址作为自己的子模块
eg:
git submodule add git@github.com:huangulong/HGLPageScroll.git GLPageScroll
在主仓库下会生成一个文件 .gitmodules
同时也会初始化相应子模块,且 .git/config 有了相关配置

2. $ git submodule init
第一次clone有子模块的仓库时,子模块的相应目录下是空的,此时就需要做该类操作
初始化本地文件,即在本地.git/config 文件中写入相关配置

3. $ git submodule update
从子模块服务器抓取最新的文件合并
也可以合并2,3两个操作,加 --recursive 可以递归检索子模块的子模块
$ git submodule update --init
$ git submodule update --init --recursive

4. $ git clone --recursive-submodules 远程地址
clone主仓库,并检索初始化所有子模块及子模块的子模块


注: 子模块下并不是一个真正的分支,而是一个commit id
(只有第一次看到的分支,但分支的其实也是一个commitid的别名;tag 亦是)

在主仓库下修改了子模块的东西,需要到相应的模块目录下取提交
$ git push origin HEAD:<name-of-remote-branch>

在命令注解中 中括号代表可选 尖括号代表必填

 

5. submodule 相关的配置文件

1 [submodule "HGLKeyPicker"]
2     path = HGLKeyPicker
3     url = git@github.com:huangulong/HGLKeyPicker.git
4 [submodule "GLPageScroll"]
5     path = GLPageScroll
6     url = git@github.com:huangulong/HGLPageScroll.git
.gitsubmodules
 1 [core]
 2     repositoryformatversion = 0
 3     filemode = true
 4     bare = false
 5     logallrefupdates = true
 6     ignorecase = true
 7     precomposeunicode = true
 8 [remote "origin"]
 9     url = ssh://ios@test.lexandera.com/Users/gulong/git.repository/XLSubModule.git
10     fetch = +refs/heads/*:refs/remotes/origin/*
11 [branch "master"]
12     remote = origin
13     merge = refs/heads/master
14 [submodule "GLPageScroll"]
15     active = true
16     url = git@github.com:huangulong/HGLPageScroll.git
17 [submodule "HGLKeyPicker"]
18     active = true
19     url = git@github.com:huangulong/HGLKeyPicker.git
.git/config

 

6. Demo 

   Github

posted @ 2021-08-06 16:35  古龙•历山大亚  阅读(124)  评论(0编辑  收藏  举报