简体   繁体   English

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

[英]Automatically access git submodules via ssh or https

Question:问题:
Is there a way to automatically checkout git submodules via the same method (ssh or https) as the main repository?有没有办法通过与主存储库相同的方法(ssh 或 https)自动检出 git 子模块?

Background:背景:

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.我们有一个非公共 gitlab 存储库( main ),它有一个子模块( utils ),它也作为非公共 gitlab 存储库托管在同一台服务器上。 Those repositories can be accessed either via ssh or https:可以通过 ssh 或 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.两种变体显然都需要不同形式的身份验证,并且取决于客户端计算机和用户,首选一种或另一种。

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

I finally solved this problem by specifying the submodules url as a relative path :我最终通过将子模块 url 指定为相对路径来解决这个问题:

So lets say your main git repository can be reached假设可以访问您的主 git 存储库

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

And the .gitmodules file looks like this: .gitmodules文件如下所示:

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

That would mean that even when you check out the main application via ssh, the submodule utils would still be accessed via https.这意味着即使您通过 ssh 检出主应用程序,子模块 utils 仍然可以通过 https 访问。

However, you can replace the absolute path with a relative one like this:但是,您可以用这样的相对路径替换绝对路径:

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

and from now on use从现在开始使用

  • either git clone --recursive https://gitlabserver.com/my/path/main.git git clone --recursive https://gitlabserver.com/my/path/main.git
  • or git clone --recursive user@gitlabserver.com:my/path/main.gitgit clone --recursive user@gitlabserver.com:my/path/main.git

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.显然,这不适用于相对 ssh 和 https 路径不同的情况,但至少对于 gitlab 托管的存储库来说是这样。

This is also handy if you (for whatever reason) mirror your repository structure at two different remote sites.如果您(无论出于何种原因)在两个不同的远程站点上镜像您的存储库结构,这也很方便。

I think I have a more general answer, but it's not quite automatic.我想我有一个更一般的答案,但它并不完全是自动的。 There's a configuration setting you can apply in ~/.gitconfig that will replace a particular URL with the one of your choosing.您可以在~/.gitconfig中应用一个配置设置,它将用您选择的 URL 替换特定的 URL。

For Github I have this in my ~/.gitconfig :对于Github ,我的~/.gitconfig有这个:

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

Here's a repository with a submodule declared with an HTTPS url that I can clone recursively using HTTPS.这是一个带有使用 HTTPS url 声明的子模块的存储库,我可以使用 HTTPS 递归克隆它。

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

OR

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

You can read about its use here: https://git-scm.com/docs/git-config#Documentation/git-config.txt-urlltbasegtinsteadOf你可以在这里阅读它的使用: https : //git-scm.com/docs/git-config#Documentation/git-config.txt-urlltbasegtinsteadOf

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM