git submodule 2022-07-14

A git submodule is a record within a host git repository that points to a specific commit in another external repository.

Submodules are very static and only track specific commits.
Submodules do not track git refs or branches and are not automatically updated when the host repository is updated.

When adding a submodule to a repository a new .gitmodules file will be created. The .gitmodules file contains meta data about the mapping between the submodule project's URL and local directory. If the host repository has multiple submodules, the .gitmodules file will have an entry for each submodule.

  • Create a submodule in a git repo
    git submodule add repoURL [localName]
git submodule add https://github.com/panjf2000/ants.git myants

this command will immediately clone the submodule panjf2000/ants into local project and rename it as myants. And this command will create a file .gitmodules as well, which shows the submodules mapping

  • Cloning git submodules
    when we want to clone a repo with submodules, git clone is not enough, because clone only work for the host repo but not submodules. so there are more things to go - init the submodules and update them
git clone /url/to/repo/with/submodules
git submodule init # 初始化本地配置文件
git submodule update # 从submodule指向的repo中抓取所有数据并检出当前父项目中列出的指定commit

or, in another simple way, use
git clone --recurse-submodules /url/to/repo/with/submodules

  • How to update submodules
    Once submodules are properly initialized and updated within a parent repository they can be utilized exactly like stand-alone repositories. This means that submodules have their own branches and history.
    • When making changes to a submodule in a parent project it is important to publish submodule changes and then update the parent repositories reference to the submodule.

    • If you want to update the submodules by get the updates from submodules' remote repo, use
      git submodule update --remote

get more:
https://git-scm.com/book/zh/v2/Git-%E5%B7%A5%E5%85%B7-%E5%AD%90%E6%A8%A1%E5%9D%97
https://www.atlassian.com/git/tutorials/git-submodule

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 资料:Git Gud: The Working Tree, Staging Area, and Local Rep...
    Mokaffee阅读 1,000评论 0 0
  • 开发中我们经常会遇到这样的情况:项目越来越大,一些通用的模块我们希望将他抽离出来作为单独的项目,以便其他项目也可以...
    feil0n9wan9阅读 20,151评论 0 24
  • 本文转载自Git-工具-子模块[https://git-scm.com/book/zh/v2/Git-%E5%B7...
    清宵寒夜阅读 3,926评论 0 0
  • Git子模块 背景信息 子模块(submodule)是Git为管理仓库共用而衍生出的一个工具,通过子模块您可以将公...
    ghostxbh阅读 779评论 0 0
  • 文章引用来源:https://blog.csdn.net/coco_1998_2/article/details/...
    driver_ab阅读 370评论 0 1