Git 子模块和 ssh 访问

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6031494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 10:57:20  来源:igfitidea点击:

Git submodules and ssh access

gitsshgit-submodules

提问by midtiby

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

我在使用包含多个子模块的 git 存储库时遇到了一些问题。

The super git repository was constructed with the commands

超级git存储库是用命令构建的

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

当另一个用户(“otheruser”)克隆超级存储库时,一切似乎都解决了。但是什么时候可以访问子模块

git submodule init
git submodule update

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

git 尝试使用“myusername”而不是“otheruser”来克隆子模块。

How to solve this problem?

如何解决这个问题呢?

采纳答案by Mark Longair

If possible, it's best to make sure that the .gitmodulesfile 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.urlto point to an SSH URL with their username, e.g.:

如果可能,最好确保.gitmodules文件包含任何人都可以克隆的存储库的 URL,通常是 agit://http://URL。然后,自己拥有 SSH 访问权限的用户可以在克隆后更改为子模块,并将 URL 更改remote.origin.url为指向其用户名的 SSH URL,例如:

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

即使在当前情况下,其他用户也应该能够做到这一点。 更新:Chris Johnsen 在下面指出,.gitmodules如果您省略用户名,那么使用 SSH URL 也是合理的,并且存储库的所有用户都将具有 SSH 访问权限 - 如果本地用户名不同,他们需要添加与上述类似的用户名和远程。

Note that the URLs in .gitmodulesare only used when initializingthe submodule. Initializing the submodule sets the config value submodule.<SUBMODULE-NAME>.urlin 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:

请注意,中的 URL.gitmodules仅在初始化子模块时使用。初始化子模块会将submodule.<SUBMODULE-NAME>.url主项目中的配置值设置为提交的任何内容.gitmodules- 这是将在第一次子模块更新时使用的值。在初始化和更新子模块之间,您还可以使用如下命令更改将用于第一次更新的 URL:

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 originwithin the submodule - at that point it's only useful to set the submodule.my-submodule.urlconfig value in the main project if you're likely to be deleting and re-updating the submodule.

事实上,如果第一次更新失败,您可能需要这样做。子模块第一次更新后,您需要更改的 URL 是origin在子模块中定义的 URL - 此时,submodule.my-submodule.url如果您可能要删除和重新设置,则只有在主项目中设置配置值才有用- 更新子模块。

回答by Ikke

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

其他用户必须更改.git/config文件才能将用户名更改为他自己的用户名。这样,git 使用正确的用户连接到服务器。

[submodule "path/to/module"]
    url = ssh://otheruser@server/pathtorepos

回答by chrisinmtown

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:

为了在开源项目中解决这个问题,我们在 .gitmodules 文件中输入了一个相对 URL。这将导致 git 根据父项目的 URL 模式克隆的 URL 克隆子模块 URL。使用相对路径巧妙地避免了完全指定协议(https、ssh)和用户名:

[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

ps 发布后我意识到我的答案是一个骗局,这是您应该使用的来源:Automatically access submodule via ssh or https

回答by midtiby

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://[email protected]/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://[email protected]/path/to/superrepos
cd superrepos
git clone ssh://[email protected]/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.

命令,git 会告诉您请求的存储库和可用的存储库不相同。但这不是致命的,您可以安全地继续。

回答by yano

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

不要在 URL 中包含用户名。git 会在你 clone/pull/etc 时提示输入用户名和密码