通过 ssh 或 https 自动访问 git 个子模块

Automatically access git submodules via ssh or https

问题:
有没有办法通过与主存储库相同的方法(ssh 或 https)自动检出 git 子模块?

背景:

我们有一个非 public git 实验室存储库 (main),它有一个子模块 (utils),它也作为非 public git 同一台服务器上的实验室存储库。可以通过 ssh 或 https:

访问这些存储库

这两种变体显然需要不同形式的身份验证,并且根据客户端计算机和用户的不同,首选其中一种。

对于顶级存储库 (main) 这不是问题,因为任何人都可以选择他或她喜欢的方法,但对于子模块,这取决于 .gitmodules 文件和因此(最初)对所有人都是一样的。
现在每个人都不必根据自己的喜好调整 .gitmodules 文件并确保他们不会意外提交这些更改,如果有一种方法可以指定服务器和回购路径,那就太好了git 选择与主仓库相同的方法,或者可以在 gitconfig.

中设置的方法

我最终通过将子模块 url 指定为 relative path:

解决了这个问题

所以假设可以访问您的主要 git 存储库

  • 或者通过 https://gitlabserver.com/my/path/main.git
  • 或通过user@gitlabserver.com:my/path/main.git

.gitmodules 文件如下所示:

[submodule "utils"]     
    path = libs/utils   
    url = https://gitlabserver.com/my/path/utils.git

这意味着即使您通过 ssh 检出主应用程序,仍可通过 https 访问子模块实用程序。

但是,您可以用这样的相对路径替换绝对路径:

[submodule "utils"]     
    path = libs/utils   
    url = ../utils.git

从现在开始使用

  • 或者git clone --recursive https://gitlabserver.com/my/path/main.git
  • git clone --recursive user@gitlabserver.com:my/path/main.git

以您想要的方式获取整个存储库结构。显然,这不适用于相对 ssh 和 https 路径不同的情况,但至少对于 gitlab 托管存储库是这样。

如果您(出于任何原因)在两个不同的远程站点镜像您的存储库结构,这也很方便。

我想我有一个更笼统的答案,但它不是很自动。您可以在 ~/.gitconfig 中应用一个配置设置,它将用您选择的一个替换特定的 URL。

对于 Github 我的 ~/.gitconfig:

[url "ssh://git@github.com/"]
    insteadOf = https://github.com/

这是一个存储库,其中包含一个使用 HTTPS url 声明的子模块,我可以使用 HTTPS 递归克隆它。

git clone --recursive https://github.com/p2/OAuth2.git

git clone --recursive git@github.com:p2/OAuth2.git

您可以在此处了解其用法:https://git-scm.com/docs/git-config#Documentation/git-config.txt-urlltbasegtinsteadOf

这里:https://git-scm.com/docs/git-clone