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

但它们并没有起作用。


当前回答

在Windows上,当git克隆时,以下步骤应该重新触发GitHub登录窗口:

在开始菜单中搜索“凭据管理器” 选择“Windows凭证” 删除所有与Git或GitHub相关的凭证

其他回答

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

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

虽然有很多答案,但我自己经常遇到用户名或密码中有特殊字符的问题。

URL编码你的用户名和密码的git,然后使用它作为URL本身的一部分(当没有安全问题)。

比如说,URL编码的用户名的值

“用户+1”是用户%2B1

和URL编码的密码值

“Welcome@1234”是欢迎%401234

然后你的GIT克隆URL看起来是这样的,

git克隆https://user%2B1:Welcome%401234@actual-git-url for-the-repo工作完美,然而, git克隆https://user+1:Welcome@1234@actual-git-url-for-the-repo给你403个错误

希望这能有所帮助。

以防万一,要在线编码URL: https://www.urlencoder.org/

我更喜欢使用GIT_ASKPASS环境为git提供HTTPS凭据。 如果将登录名和密码导出为USR和PSW变量,则以下脚本不会在历史记录中留下密码的痕迹,并且磁盘+不容易受到密码中的特殊字符的影响:

GIT_ASKPASS=$(mktemp) && chmod a+rx $GIT_ASKPASS && export GIT_ASKPASS

cat > $GIT_ASKPASS <<'EOF'
#!/bin/sh
exec echo "$PSW"
EOF

git clone https://${USR}@example.com/repo.git

注意:此处doc标记“EOF”周围的单引号意味着临时脚本保存$PSW字符,而不是PSW变量的密码/扩展值

如果你有凭据存储在任何类型的凭据帮助中,但没有将凭据帮助配置为全局激活,你可以像这样克隆:

git clone https://git.example.com/ns/repo.git --config=credential.helper=store

这将在克隆之前在本地设置凭据帮助配置(仅针对新的回购)。

在Windows上,当git克隆时,以下步骤应该重新触发GitHub登录窗口:

在开始菜单中搜索“凭据管理器” 选择“Windows凭证” 删除所有与Git或GitHub相关的凭证