通过 ssh 或 https 自动访问 git 子模块 [英] Automatically access git submodules via ssh or https

查看:23
本文介绍了通过 ssh 或 https 自动访问 git 子模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Question:
Is there a way to automatically checkout git submodules via the same method (ssh or https) as the main repository?

背景:

我们有一个非公共 gitlab 存储库 (main),它有一个子模块 (utils),它也作为非公共 gitlab 存储库托管在同一台服务器上.可以通过 ssh 或 https 访问这些存储库:

We have a non-public gitlab repository (main) that has a submodule (utils) which is also hosted as a non-public gitlab repository on the same server. Those repositories can be accessed either via ssh or https:

  • user@gitlabserver.com:my/path/repo.git
  • https://gitlabserver.com/my/path/repo.git

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

Both variants obviously require different forms of authentication and depending on the client computer and the user, one or the other is preferred.

对于顶级存储库(main)这不是问题,因为任何人都可以选择他或她喜欢的方法,但对于子模块,这取决于 .gitmodules 文件,因此(最初)对所有人都相同.
现在不是每个人都必须将 .gitmodules 文件调整为他们喜欢的任何内容并确保他们不会意外提交这些更改,如果有一种方法可以指定服务器和 repo 路径并且 git 选择与主 repo 使用的方法相同,或者可以在 gitconfig 中设置的方法.

For the top level repository (main) that is not an issue, as anyone can choose the method he or she prefers, but for the sub module this depends on the .gitmodules file and hence is (initially) the same for all.
Now instead of everyone having to adapt the .gitmodules file to whatever they prefer and make sure they don't accidentally commit those changes, it would be nice, if there was a way to just specify the server and repo path and git chooses either the same method that is used for the main repo, or something that can be set in gitconfig.

推荐答案

我终于通过将子模块 url 指定为 相对路径:

I finally solved this problem by specifying the submodules url as a relative path:

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

So lets say your main git repository can be reached

  • 通过 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 检查主应用程序,子模块 utils 仍然可以通过 https 访问.

That would mean that even when you check out the main application via ssh, the submodule utils would still be accessed via https.

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

However, you can replace the absolute path with a relative one like this:

[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 托管的存储库是这种情况.

to get the whole repository structure which ever way you want. Obviously that doesn't work for cases where the relative ssh and the https paths are not the same, but at least for gitlab hosted repositories this is the case.

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

This is also handy if you (for whatever reason) mirror your repository structure at two different remote sites.

这篇关于通过 ssh 或 https 自动访问 git 子模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆