Automatically access git submodules via ssh or https

后端 未结 2 961
生来不讨喜
生来不讨喜 2021-01-30 01:59

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

Background:

2条回答
  •  醉酒成梦
    2021-01-30 02:51

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

    So lets say your main git repository can be reached

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

    And the .gitmodules file looks like this:

    [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.

    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
    • or git 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.

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

提交回复
热议问题