我知道如何为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.

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

其他回答

git config --global core.askpass

在克隆之前先运行这个,应该是固定的!

如果这些参数正在产生问题,请使用ur的特殊情况替换它们:

 !   #   $   &   '   (   )   *   +   ,   /   :   ;   =   ?   @   [   ]

%21 %23 %24 %26 %27 %28 %29 %2A %2B %2C %2F %3A %3B %3D %3F %40 %5B %5D

举个例子

实际URL: https://usern@me:p@ssword@git/reponame.git

使用的解决方案URL: https://usern%40me:p%40ssword@git/reponame.git

这是一个很棒的Stack Overflow问题,答案非常有启发性,因此我能够解决最近遇到的一个烦人的问题。

我工作的组织使用Atlassian的BitBucket产品(不是Github),本质上是他们的Github版本,因此存储库可以完全在前提下得到保护。我遇到了与@coordinate类似的问题,因为我签出的新存储库需要密码。我的证书被保存在所有BitBucket项目的全球范围内,所以我不确定是什么原因导致了证书的丢失。

简而言之,我可以输入以下GIT命令(只提供我的用户名),然后提示GIT的凭证管理器提示我输入密码,然后我就可以保存密码了。

git clone https://user@code.domain.org/git/[organization]/[team]/[repository.git]

注意:括号内的目录子路径只是指内部引用,并且会因你而异!

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

在@ 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.

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