Git 子模块 URL 不包括用户名?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7714326/
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 12:02:11  来源:igfitidea点击:

Git submodule URL not including username?

gitgit-submodules

提问by MidnightLightning

I have a git repository set up with several submodules, which creates a .gitmodulesfile that is a tracked file in the parent repository. However, there are other developers wanting to work on this repository, and check out the submodules. But currently the URLs for the remote submodule repositories contain my username; in the .gitmodulesfile it's something like:

我有一个 git 存储库,它设置了几个子模块,它.gitmodules在父存储库中创建一个作为跟踪文件的文件。但是,还有其他开发人员想要在这个存储库上工作,并查看子模块。但目前远程子模块存储库的 URL 包含我的用户名;在.gitmodules文件中它是这样的:

[submodule foo]
  path = sub/foo
  url = https://[email protected]/git/foo.git

Obviously other developers can't fetch from example.comas myuser(they don't have my password); how can I have one main repository that multiple developers can pull/push to, and allow them to have individual access to the submodules (setting up a single username they all share on the submodule host server would work, but is not good user management)?

显然其他开发人员无法从example.comas myuser(他们没有我的密码)获取;我怎样才能拥有一个多个开发人员可以拉/推到的主存储库,并允许他们单独访问子模块(设置他们在子模块主机服务器上共享的单个用户名可以工作,但不是很好的用户管理) ?

采纳答案by Mark Longair

If I understand correctly, you're using HTTP basic authentication over HTTPS to allow only particular developers to access the repository. In that case, you can commit a .gitmodulesthat looks like:

如果我理解正确的话,您将通过 HTTPS 使用 HTTP 基本身份验证,以仅允许特定开发人员访问存储库。在这种情况下,您可以提交.gitmodules如下所示的代码:

[submodule foo]
  path = sub/foo
  url = https://example.com/git/foo.git

... i.e. without a user name, and then tell each developer to put their username and password in their ~/.netrcfile. (If you're using Windows, then there is some good advice on that here.) A simple .netrcfile might look like:

... 即没有用户名,然后告诉每个开发人员将他们的用户名和密码放在他们的~/.netrc文件中。(如果您使用的是 Windows,那么这里有一些很好的建议。)一个简单的.netrc文件可能如下所示:

machine example.com
  login myusername
  password areamandyingtotellsomeonehiscoolpassword


Update: An alternative, which doesn't involve using .netrc, would be the following:

更新:不涉及使用的替代方法.netrc如下:

Again, remove the user name from the URL in .gitmodulesand commit and push that change. When someone clones the repository they would first run:

同样,从 URL 中删除用户名.gitmodules并提交并推送该更改。当有人克隆存储库时,他们将首先运行:

git submodule init

... which will set the config option submodule.sub/foo.urlto the URL in .gitmodules. However, the initstep won't clone the submodule into place until you do git submodule update, so you can do:

...这将设置配置选项submodule.sub/foo.url在URL .gitmodules。但是,该init步骤在您执行之前不会将子模块克隆到位git submodule update,因此您可以执行以下操作:

git config submodule.sub/foo.url https://myuser:[email protected]/git/foo.git

... and then:

... 进而:

git submodule update

To clone the submodules with the right user name. Note that then your username and password for HTTP authentication will be stored in your git config.

使用正确的用户名克隆子模块。请注意,您用于 HTTP 身份验证的用户名和密码将存储在您的 git 配置中。

回答by AntonK

Actually you can specify a "relative" path to a submodule in .gitconfig:

实际上,您可以在以下位置指定子模块的“相对”路径.gitconfig

[submodule foo]  
path = sub/foo  
url = ./git/foo.git

This url will reference same host (https://example.com) as the repository itself.

此 url 将引用与存储库本身相同的主机 ( https://example.com)。

回答by user698116

This threaded helped me, but I would add that after you modify the content of the file .gitmodulesyou need to execute the following command so git will pick it up:

这个线程对我有帮助,但我想补充一点,在修改文件.gitmodules的内容后,您需要执行以下命令,以便 git 将其提取:

git submodule sync

回答by rhamilton

Antonk's answer won't work for teams because an individual's .gitconfig or .git/config is not under version control.

Antonk 的答案不适用于团队,因为个人的 .gitconfig 或 .git/config 不受版本控制。

However it did lead me to experimenting with the url in .gitmodules.

然而,它确实让我尝试使用 .gitmodules 中的 url。

[submodule foo]
path = sub/foo
url = ../foo.git

worked for me, assuming that the submodule repo is in the same url as the parent.

对我来说有效,假设子模块 repo 与父模块位于相同的 url 中。

回答by mkilic

You can use ssh authentication.

您可以使用 ssh 身份验证。

just replace this

只需更换这个

[submodule foo]
  path = sub/foo
  url = https://[email protected]/git/foo.git

with this

有了这个

[submodule foo]
  path = sub/foo
  url = [email protected]:git/foo.git

回答by Roman

I had this problem in combination with Gerrit(via ssh).
Removing the username worked,
so my .gitmoduleslooks like this now:

我在结合Gerrit(通过 ssh)时遇到了这个问题。
删除用户名有效,
所以我的.gitmodules现在看起来像这样:

[submodule "sub/module1"]
    path = sub/module1
    url = ssh://servername:29418/module1

回答by bviktor

You can try .insteadOf, e.g.

你可以试试.insteadOf,例如

git config --global url."ssh://[email protected]:29418/".insteadOf "ssh://gerrit.foobar.com:29418/"

Each developer will have to do this once on each computer they work on.

每个开发人员都必须在他们工作的每台计算机上执行一次此操作。

回答by Giorgos Myrianthous

Modify your .gitmodulesand remove the username from the url:

修改您.gitmodules的用户名并将其从 中删除url

[submodule foo]
  path = sub/foo
  url = https://example.com/git/foo.git

And finally:

最后:

cd foo/
git submodule init
git submodule update

# Will be prompted to provide username and password

回答by Colin

Alternatively you can use HTTPSin place of SSHwhich will prompt you for your username and password:

或者,您可以使用HTTPS代替SSH,这将提示您输入用户名和密码:

enter image description here

在此处输入图片说明