我可以使用 --recursive 选项(对于子模块)向 git clone 添加用户名和密码吗?

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

Can I add a username & password to git clone using the --recursive option (for submodules)?

gitgit-submodules

提问by Neophyte

I am using git to clone a repo via https thus:

我正在使用 git 通过 https 克隆一个 repo,因此:

git clone https://username:password@alocation/git/repo.git

This is fine but it has a large number of subrepos to clone as well so I am using the --recursiveoption.

这很好,但它也有大量子存储库要克隆,所以我正在使用该--recursive选项。

Problem here is that for the top level it takes the username & pass specified but for each sub repo it asks for the details again so I get the following:

这里的问题是,对于顶级,它需要指定的用户名和通行证,但对于每个子存储库,它再次询问详细信息,因此我得到以下信息:

C:>git clone --recursive https://username:password@alocation/git/repo.git
Cloning into repo...
remote: Counting objects: 15, done.
remote: Compressing objects: 100% (15/15), done.
remote: Total 15 (delta 8), reused 0 (delta 0)
Unpacking objects: 100% (15/15), done.
Submodule 'sub1' (https://alocation/git/repo.sub1) registered for path 'sub1'
Submodule 'sub2' (https://alocation/git/repo.sub2) registered for path 'sub2'
Submodule 'sub3' (https://alocation/git/repo.sub3) registered for path 'sub3'
Submodule 'sub4' (https://alocation/git/repo.sub4) registered for path 'sub4'
Cloning into sub1...
Username:
Password:
remote: Counting objects: 10, done.
remote: Compressing objects: 100% (8/8), done.
remote: Total 10 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (10/10), done.
Username:
..............................etc....

Can I give the username & password separably so I don't have to enter each time.

我可以分开提供用户名和密码吗,这样我就不必每次都输入了。

Obvious use case is for a deep subrepo structure like this where there are lots of files. Entering the same data for each subrepo is going to get tedious & error prone.

明显的用例适用于像这样有很多文件的深层子仓库结构。为每个子仓库输入相同的数据会变得乏味且容易出错。

回答by VonC

The credentials should be valid for any submodules with an address supposed to accept them.

凭据应该对任何具有接受它们的地址的子模块有效。

One only case where it could fail is when the .gitmodulesfile points to another repo, as illustrated by the case 214of the Hunch Kod project.

唯一可能失败的情况是.gitmodules文件指向另一个存储库,如Hunch Kod 项目的案例 214所示。

To make sure those credentials are passed to every request to 'alocation' server, you don't need to tweak anything in Git, but this is probably a ssh, curl or http proxy settings.

为了确保将这些凭据传递给“alocation”服务器的每个请求,您无需在 Git 中进行任何调整,但这可能是 ssh、curl 或 http 代理设置。

I would rule out ssh (alocation wouldn't execute anything as 'username' but would rather have a dedicated user account).

我会排除 ssh(alocation 不会以“用户名”的形式执行任何操作,而是拥有一个专用的用户帐户)。

Check your http_proxy and https_proxy environment variable if you have a proxy.

如果您有代理,请检查您的 http_proxy 和 https_proxy 环境变量。

But try also a simple curl https://alocation/git/repo.git:
With a $HOME/.netrc(%HOME%\_netrcon Windows), you can specify the login/password expected.

但也可以尝试一个简单的方法curl https://alocation/git/repo.git
使用$HOME/.netrc(%HOME%\_netrc在 Windows 上),您可以指定预期的登录名/密码。

machine alocation
  login username
  password mypassowrd

If that work for https://alocation/git/repo.git(i.e. without asking you for a username and password), it will work for any other repo( here submodules).

如果这适用于https://alocation/git/repo.git(即不要求您提供用户名和密码),它将适用于任何其他回购(此处为子模块)。