我知道如何为HTTPS请求提供用户名和密码,就像这样:

git clone https://username:password@remote

但是我想知道如何像这样为遥控器提供用户名和密码:

git clone git@remote.git

我是这样尝试的:

git clone username:password@git@remote.git
git clone git@username:password@remote.git
git clone git@remote.git@username:password

但它们并没有起作用。


当前回答

我用以下方法解决了这个问题:

其他回答

在@ bassetassen的回答的评论中,@plosco提到,你可以使用git克隆https://<token>@github.com/username/repository.git从GitHub克隆至少。我想我应该扩展一下如何做到这一点,以防有人遇到这个答案,就像我在尝试自动克隆时所做的那样。

GitHub有一个关于如何做到这一点的非常方便的指南,但它没有涵盖如果你想把它都包含在一行中以实现自动化的目的该怎么做。它警告说,将令牌添加到克隆URL将以明文形式存储在.git/config中。这显然对几乎每个用例都是一个安全风险,但由于我计划在完成时删除回购并撤销令牌,所以我不在乎。

1. 创建令牌

GitHub有一个关于如何获得令牌的完整指南,但这里是TL;DR。

转到设置>开发人员设置>个人访问令牌(这里有一个直接链接) 点击“Generate a New Token”并再次输入密码。(这是另一个直接链接) 设置它的描述/名称,检查“回购”权限,并点击页面底部的“生成令牌”按钮。 在离开页面之前复制新令牌

2. 克隆回购

与@plosco给出的命令相同,git克隆https://<token>@github.com/<username>/<repository>.git,只需将<token>, <username>和<repository>替换为您的任何信息。

如果你想克隆它到一个特定的文件夹,只需在最后插入文件夹地址,像这样:git克隆https://<token>@github.com/<用户名>/<repository.git> <文件夹>,其中<文件夹>是,你猜对了,文件夹要克隆到!你当然可以用.., ~,等等,就像你在其他地方一样。

3.不留痕迹

并非所有这些都是必要的,这取决于您所做的事情有多敏感。

You probably don't want to leave that token hanging around if you have no intentions of using it for some time, so go back to the tokens page and hit the delete button next to it. If you don't need the repo again, delete it rm -rf <folder>. If do need the repo again, but don't need to automate it again, you can remove the remote by doing git remote remove origin or just remove the token by running git remote set-url origin https://github.com/<username>/<repository.git>. Clear your bash history to make sure the token doesn't stay logged there. There are many ways to do this, see this question and this question. However, it may be easier to just prepend all the above commands with a space in order to prevent them being stored to begin with.

请注意,我不是专业人士,所以上述内容可能不安全,因为任何类型的法医工作都不会留下任何痕迹。

我用以下方法解决了这个问题:

2021年8月13日后必须使用

Git克隆 https://username:token@github.com/username/repository.git

生成令牌:

设置>>开发人员设置>>个人访问令牌>>生成新的令牌

去推

克隆成功后,下次当你做git推送时,你将不必再次提到用户名。你可以在记事本中打开.git/config文件来确认

[remote "origin"]
    url = https://username:tokenxxxxxxxxxx@github.com/username/repo.git

user@host:path/to/repo格式告诉Git使用ssh以用户名user登录主机。从git帮助克隆:

另一种类似scp的语法也可以用于ssh协议: [user@] host.xz: / / repo.git /路径

@前面的部分是用户名,身份验证方法(密码、公钥等)由ssh决定,而不是Git。Git无法将密码传递给ssh,因为ssh甚至可能不使用密码,这取决于远程服务器的配置。

使用ssh-agent可以避免一直输入密码

如果您不想一直输入ssh密码,典型的解决方案是生成一个公钥/私钥对,将公钥放在~/。打开远程服务器上的Ssh /authorized_keys文件,并将私钥加载到Ssh -agent中。还可以参考配置Git通过SSH登录一次,GitHub的SSH密钥密码帮助页面,gitolite的SSH文档和Heroku的SSH密钥文档。

在GitHub(或Heroku或…)

如果你在GitHub或Heroku等地方有多个帐户,你将有多个ssh密钥(每个帐户至少一个)。要选择要作为哪个帐户登录,必须告诉ssh要使用哪个私钥。

例如,假设你有两个GitHub帐户:foo和bar。foo的ssh键是~/。Ssh /foo_github_id, bar的Ssh密钥为~/. Ssh /bar_github_id。你需要用你的foo帐户访问git@github.com:foo/foo.git,用你的bar帐户访问git@github.com:bar/bar.git。您可以在~/.ssh/config中添加以下内容:

Host gh-foo
    Hostname github.com
    User git
    IdentityFile ~/.ssh/foo_github_id
Host gh-bar
    Hostname github.com
    User git
    IdentityFile ~/.ssh/bar_github_id

然后,您将复制两个存储库,如下所示:

git clone gh-foo:foo/foo.git  # logs in with account foo
git clone gh-bar:bar/bar.git  # logs in with account bar

完全避免ssh

一些服务提供HTTP访问作为ssh的替代方案:

GitHub: https://username:password@github.com/username/repository.git Gitorious: https://username:password@gitorious.org/project/repository.git Heroku:请参阅此支持文章。

警告:将您的密码添加到克隆URL将导致Git将您的明文密码存储在. Git /config中。要在使用HTTP时安全地存储密码,请使用凭据帮助器。例如:

git config --global credential.helper cache
git config --global credential.https://github.com.username foo
git clone https://github.com/foo/repository.git

上述操作将导致Git每15分钟(默认情况下)询问一次您的密码。详见git帮助凭证。

根据Michael Scharf的评论:

你可以省略密码,这样它就不会被记录在你的Bash历史文件中:

git clone https://username@github.com/username/repository.git

它将提示您输入密码。

或者,你可以使用:

git clone https://username:password@github.com/username/repository.git

我从GitHub存储库中找到了这种方法。