8. git-submodule 子模块

简介: Git Submodule 允许一个 git 仓库,作为另一个 git 仓库的子目录,并且保持父项目和子项目相互独立。父项目:外层项目子项目:里面的项目。

Git Submodule 允许一个 git 仓库,作为另一个 git 仓库的子目录,并且保持父项目和子项目相互独立。


父项目:外层项目


子项目:里面的项目。


常用命令



git submodule 涉及的常用功能有:


  • git clone <repository> –recursive :递归的方式克隆整个项目


  • git submodule add <repository> <path> :添加子模块


  • git submodule init :初始化子模块


  • git submodule update :更新子模块


  • git submodule foreach git pull: 拉取所有子模块


  • git submodule foreach git checkout -- .: 所有子模块进行 checkout -- . 操作。

NAME
       git-submodule - Initialize, update or inspect submodules
SYNOPSIS
       git submodule [--quiet] add [<options>] [--] <repository> [<path>]
       git submodule [--quiet] status [--cached] [--recursive] [--] [<path>...]
       git submodule [--quiet] init [--] [<path>...]
       git submodule [--quiet] deinit [-f|--force] (--all|[--] <path>...)
       git submodule [--quiet] update [<options>] [--] [<path>...]
       git submodule [--quiet] summary [<options>] [--] [<path>...]
       git submodule [--quiet] foreach [--recursive] <command>
       git submodule [--quiet] sync [--recursive] [--] [<path>...]
       git submodule [--quiet] absorbgitdirs [--] [<path>...]
DESCRIPTION
       Inspects, updates and manages submodules.
       For more information about submodules, see gitsubmodules(7).


子模块的添加

git submodule add <url> <path>


其中,url 为子模块的路径,path 为该子模块存储的目录路径。


执行成功后,git status 会看到项目中修改了.gitmodules,并增加了一个新文件夹(为刚刚添加的路径)


使用命令 git status 可以看到多了两个需要提交的文件,其中 .gitmodules 指定submodule 的主要信息,包括子模块的路径和地址信息,moduleA 指定了子模块的commit id,使用 git diff 可以看到这两项的内容。


然后和往常一样进行 add 和 commit 即可.


需要注意的是,父项目的 git 并不会记录 submodule 的文件变动,它是按照 commit id 指定 submodule 的 git header,所以 .gitmodules 和 moduleA 这两项是需要提交到父项目的远程仓库的。


克隆带子模块的版本库


方法一:先 clone 父项目,再初始化 submodule,最后更新 submodule,初始化只需要做一次,之后每次只需要直接 update 就可以了,需要注意 submodule 默认是不在任何分支上的,它指向父项目存储的 submodule commit id。

git clone project.git project2 && cd project2 
git submodule init 
git submodule update


或者

git clone project.git project2  && cd project2 
git submodule update --init --recursive


方法二:采用递归参数 –recursive,需要注意同样 submodule 默认是不在任何分支上的,它指向父项目存储的 submodule commit id。

git clone project.git project3 –recursive


子模块的更新


子模块的维护者提交了更新后,使用子模块的项目必须手动更新才能包含最新的提交。


在项目中,进入到子模块目录下,执行 git pull 更新,查看 git log 查看相应提交。


完成后返回到项目目录,可以看到子模块有待提交的更新,使用 git add,提交即可。


从存储库中删除所有 Git 缓存的子模块(Deleting all Git cached submodules from repository)

# deinit all submodules from .gitmodules
git submodule deinit .
# remove all submodules (`git rm`) from .gitmodules
git submodule | cut -c43- | while read -r line; do (git rm "$line"); done
# delete all submodule sections from .git/config (`git config --local --remove-section`) by fetching those from .git/config
git config --local -l | grep submodule | sed -e 's/^\(submodule\.[^.]*\)\(.*\)/\1/g' | while read -r line; do (git config --local --remove-section "$line"); done
# manually remove leftovers
rm .gitmodules
rm -rf .git/modules


I do not know for server synchronisation. It could be done automatically with next commit, or we might need those commands:

git submodule sync
git submodule update --init --recursive --remote


一个示例


git clone git@gitee.com:mabuo/html.git
# 添加子模块, 并进行一次提交 f'f
git submodule add git@gitee.com:mabuo/html.git lala
git add .
git commit -m 'add submodule'


Git出现 fatal: Pathspec 'xxx' is in submodule 解决方案



由于某个目录是一个 git 项目.


使用 git add 后只增加了文件夹,但是没有文件。手动 Add 里面单个文件则报出错误信息:


fatal: Pathspec 'xxx' is in submodule


解决方案:


发现 vendor/crazyfd 下面并没有 .git 文件


所以使用下面命令:

git rm -rf --cached vendor/crazyfd/yii2-qiniu 
git add vendor/crazyfd/yii2-qiniu/*


参考



Git 出现 fatal: Pathspec 'xxx' is in submodule 解决方案


https://blog.csdn.net/JaredFu/article/details/53116578


git submodule的使用_Jacob-wj的博客-CSDN博客_git submod


https://blog.csdn.net/wangjia55/article/details/24400501


目录
相关文章
|
开发工具 git
Git添加子模块(submodule)
Git添加子模块(submodule)
897 0
|
Linux 开发工具 git
还不会 Git 子模块操作?一文教你学会 git submodule 的增、删、改、查!
还不会 Git 子模块操作?一文教你学会 git submodule 的增、删、改、查!
417 0
|
开发工具 git
git clone 含有子模块的项目
git clone 含有子模块的项目
941 0
|
开发工具 git
手把手教你把 Git 子模块更新到主项目
本文以 skywalking-rocketbot-ui子模块合并到 skywalking 为例,手把手教你如何把 Git 子模块更新到主项目中去。 首先,把fork的skywalking项目克隆到本地:
372 0
|
API Docker 容器
用Git子模块和Docker Compose实现高效开发工作流
本文讲的是用Git子模块和Docker Compose实现高效开发工作流,【编者的话】搭建开发环境一直让程序员们头疼,本文使用Git子模块和Docker Compose实现高效率的开发工作流,让程序员能够轻松搭建出开发环境,把精力投入到需要开发的应用本身。
2178 0
|
开发工具 git
Git 工具 - 子模块
子模块 有种情况我们经常会遇到:某个工作中的项目需要包含并使用另一个项目。 也许是第三方库,或者你独立开发的,用于多个父项目的库。 现在问题来了:你想要把它们当做两个独立的项目,同时又想在一个项目中使用另一个。
1212 0
|
1月前
|
开发工具 git
Git版本控制工具合并分支merge命令操作流程
通过以上步聚焦于技术性和操作层面指南(guidance), 可以有效管理项目版本控制(version control), 并促进团队协作(collaboration).
220 15
|
4月前
|
安全 开发工具 git
git的常用操作命令
git的常用操作命令
324 57
|
2月前
|
存储 缓存 开发工具
Git stash命令的详细使用说明及案例分析。
通过上述案例,我们看到stash命令能够在不丢失进度的情况下,帮助开发者临时切换开发上下文,这在处理多个任务或紧急bug时特别有用。正确使用Git stash可以大大提高开发的灵活性和效率。
961 0