前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >git知识库 - submodule 本质是个啥

git知识库 - submodule 本质是个啥

原创
作者头像
逝兮诚
修改2020-06-13 16:12:33
1K0
修改2020-06-13 16:12:33
举报
文章被收录于专栏:代码人生代码人生

一日,前端大神突然指出,我提供的 git 仓库的文件列表接口中,一个 submodule 类型文件为什么没有对应链接,没有链接他无法跳转到对应仓库页面,让我排查一下这个仓库和对应接口,看是发生了什么问题。我当时一方,对大神发出灵魂三问。 submodule 是什么,项目链接又是什么,为什么要链接呢?大神看看我一脸茫然的样子,沉思片刻之后,还是让我先google一下。

我快速的开始了 google,原来 submodule 是子仓库的意思,即在仓库中引入一个新的 git 仓库。它在 git 仓库中是一个带有 .git 信息的文件夹。如在仓库中 clone 一个网上的仓库后,执行 git submodule add 添加该仓库为子仓库。add 子仓库时,git 仓库会在添加一条子仓库索引,标记该文件夹为子仓库,同时会在根目录创建一个 .gitmodules 文件,存放子仓库与 url 数据。

代码语言:txt
复制
✗ git submodule add https://github.com/programluo/stock_tool.git stock_tool
✗ ll -a
total 16
drwxr-xr-x   6 marxluo  staff   192B  6 13 13:29 .
drwxr-xr-x   3 marxluo  staff    96B  6 13 13:20 ..
drwxr-xr-x  12 marxluo  staff   384B  6 13 13:31 .git
-rw-r--r--   1 marxluo  staff    96B  6 13 13:29 .gitmodules
-rw-r--r--   1 marxluo  staff    25B  6 13 13:21 readme.md
drwxr-xr-x  14 marxluo  staff   448B  6 13 13:21 stock_tool

执行 git ls-files --stage 可以看到仓库的索引信息,索引类型 160000,代表是子仓库的索引。

代码语言:txt
复制
✗ git ls-files --stage
100644 a7c8709d53afc9c52f66fcac2e238462975a8a94 0    .gitmodules
100644 13e5109d4a11e8c09e27a81c056503a9bc8e4ad1 0    readme.md
160000 04b2bdbefdc780349489f0b74b0a5c0050dc2c08 0    stock_tool

.gitmodules 文件内容

代码语言:txt
复制
✗ cat .gitmodules
[submodule "stock_tool"]
    path = stock_tool
    url = https://github.com/programluo/stock_tool.git

故而 submodule 的本质是子仓库索引 + .gitmodules文件。

回到最开始的现象,为什么会出现没有 url 的子仓库呢?我 clone 那个仓库数据,打开发现存在子仓库索引,但是没有 gitmodules 文件,那么当时可能是把 .gitmodules 文件误删除了;或者是使用下面这种错误的方式添加子项目。

我无意间发现了这种错误的方式,clone 子仓库后使用 add -A,会添加子仓库索引,但是不会添加 .gitmodules 文件,而且不能执行 git submodule add 来添加子仓库 url。只能手动添加 .gitmodules 文件或者通过 git restore --staged 来删除索引。下面是错误演示

代码语言:txt
复制
~: git clone https://github.com/programluo/stock_tool.git
~: git add -A
warning: adding embedded git repository: stock_tool
hint: You've added another git repository inside your current repository.
hint: Clones of the outer repository will not contain the contents of
hint: the embedded repository and will not know how to obtain it.
hint: If you meant to add a submodule, use:
hint:
hint:     git submodule add <url> stock_tool
hint:
hint: If you added this path by mistake, you can remove it from the
hint: index with:
hint:
hint:     git rm --cached stock_tool
hint:
hint: See "git help submodule" for more information.

# 不存在 .gitmodules 文件
✗ git submodule
fatal: no submodule mapping found in .gitmodules for path 'stock_tool'

# 无法添加 stock_tool 子仓库
✗ git submodule add https://github.com/programluo/stock_tool.git stock_tool
'stock_tool' already exists in the index

# 子仓库索引又存在
✗ git ls-files --stage
100644 13e5109d4a11e8c09e27a81c056503a9bc8e4ad1 0    readme.md
160000 04b2bdbefdc780349489f0b74b0a5c0050dc2c08 0    stock_tool

这里可以回答一下我之前出现的问题了,是由于测试仓库无意的 clone 了一个外部仓库,可能通过 git add -A 错误的添加了子仓库索引,而不是通过 git submodule add 添加 url 信息,导致没有 .gitmodules 文件,子仓库没有对应的 url 数据,属于数据错误。后来前端给这种没有 url 的子仓库文件夹点击事件加上了错误提示,“未设置子仓库 url,无法跳转到对于仓库”。

查看文档

https://stackoverflow.com/questions/2223308/how-to-remove-a-file-from-the-index-in-git

https://stackoverflow.com/questions/1992018/git-submodule-update-needed-only-initially/2227598#2227598

https://stackoverflow.com/questions/16574625/how-do-i-add-files-in-git-to-the-path-of-a-former-submodule/16581096#16581096

https://git-scm.com/docs/gitsubmodules

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档