Git submodules and ssh access

2019-01-10 22:35发布

I have some trouble with a git repository that contains several submodules.

The super git repository was constructed with the commands

mkdir projectname
cd projectname
git init
git submodule add ssh://myusername@server/pathtorepos

When a different user ("otheruser") then clones the super repository everything seems to work out. But when it is time to get access to the submodule

git submodule init
git submodule update

git tries to clone the submodule using "myusername" instead of "otheruser".

How to solve this problem?

5条回答
虎瘦雄心在
2楼-- · 2019-01-10 22:58

Do not include the username in the URL. git will prompt for the username and password when you clone/pull/etc

查看更多
欢心
3楼-- · 2019-01-10 23:01

To address this in an open-source project we enter a RELATIVE URL in the .gitmodules file. This will cause git to clone the submodule URL based on the URL being cloned by the parent project's URL pattern. Using a relative path neatly avoids specifying protocol (https, ssh) and username entirely:

[submodule "my/tests/schemas"]
    path = my/tests/schemas
    url = ../my-schema

p.s. after posting I realized that my answer is a dupe, here is the source you should use: Automatically access submodule via ssh or https

查看更多
对你真心纯属浪费
4楼-- · 2019-01-10 23:01

Just for reference, the solution I ended up using is the following. It is actually possible for others to check out the existing repository.

When I need to check out the repository it can be done with the commands

git clone ssh://myusername@server.dk/path/to/superrepos
cd superrepos
git submodule init
git submodule update

For others to check out the super repository, the following set of commands is used. The only difference is the manual cloning of the other repository

git clone ssh://otheruser@server.dk/path/to/superrepos
cd superrepos
git clone ssh://otheruser@server.dk/path/to/other/repos
git submodule init
git submodule update

Note that after issuing the

git submodule init

command, git will tell you that the requested repository and the available is not identical. But this is not fatal and you can safely continue.

查看更多
我命由我不由天
5楼-- · 2019-01-10 23:14

If possible, it's best to make sure that the .gitmodules file contains a URL for the repository that can be cloned by anyone, typically either a git:// or http:// URL. Then users that have SSH access themselves can change into the submodule after cloning and change the URL in remote.origin.url to point to an SSH URL with their username, e.g.:

 cd my-submodule
 git remote set-url origin otheruser@server:/pathtorepos

The other user should be able to do that even in the current situation. Update: Chris Johnsen points out below that it's also reasonable to use an SSH URL in .gitmodules if you omit the username and all the users of the repository will have SSH access - they'll need to add their username similarly to the above if it differs locally and remotely.

Note that the URLs in .gitmodules are only used when initializing the submodule. Initializing the submodule sets the config value submodule.<SUBMODULE-NAME>.url in the main project to whatever's committed in .gitmodules - this is the value that will be used on the first submodule update. Between initializing and updating the submodule, you can also change this URL that will be used for that first update with a command like:

git config submodule.my-submodule.url otheruser@server:/pathtorepos

Indeed, you may need to do this if the first update fails. Once the submodule has been updated for the first time, the URL you need to change is that defined for origin within the submodule - at that point it's only useful to set the submodule.my-submodule.url config value in the main project if you're likely to be deleting and re-updating the submodule.

查看更多
仙女界的扛把子
6楼-- · 2019-01-10 23:14

The other user has to alter the .git/config file to change the user name to his own username. That way, git uses the right user to connect to the server.

[submodule "path/to/module"]
    url = ssh://otheruser@server/pathtorepos
查看更多
登录 后发表回答