美文网首页
7. git-submodule 子模块

7. git-submodule 子模块

作者: acc8226 | 来源:发表于2020-03-06 21:27 被阅读0次

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: 拉取所有子模块
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

相关文章

  • 7. git-submodule 子模块

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

  • git submodule 使用小结

    原文链接 http://blog.gezhiqiang.com/2017/03/08/git-submodule/...

  • Python:7.模块

    标准模板 作用域 第三方包管理 包管理工具pip3 安装常用模块 Anaconda内置很多非常有用的库下载完之后直...

  • 【Perl】——7. 模块

    2021.2.7持续更新中。。。 《Perl语言入门》 1. 简介 模块是一些已经打包好的用于解决某些问题的方法集...

  • 7.模块和包

    模块 每一个Python脚本都可以被当成是一个模块,模块以磁盘文件的形式存在,当一个模块变得过大,并且驱动了太多功...

  • Vuex4.x(五)模块管理(上)

    module 的用法 一开始以为模块只能是一层,仔细看了官网才发现,原来模块还可以有n层,即子模块、子子模块、子子...

  • “智能预警”产品设计—练习

    1、定义产品功能模块以及功能的原则 2、分别列出功能模块的所有子功能模块,阐述子功能的定义 3、画出该功能的模块的...

  • node一些模块

    child_process模块 通过child_process模块创建子进程 childProcess.exec(...

  • 自己现在工作应该关注的点

    1.热修复 2.代码封装 3.高级UI 4.性能优化 5.设计模式 6.模块化 7.插件化

  • 周报丨Insight Chain(INB)公链&生态周报(201

    摘要:子链PBFT共识模块开发完成。 ▶公链◀ 一、INB公链1.子链PBFT共识模块数据结构优化修改。2.子链P...

网友评论

      本文标题:7. git-submodule 子模块

      本文链接:https://www.haomeiwen.com/subject/isetrhtx.html