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

但它们并没有起作用。


当前回答

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

其他回答

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

根据Michael Scharf的评论:

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

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

它将提示您输入密码。

或者,你可以使用:

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

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

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

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

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

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

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/

如果您正在使用http/https,并且希望完全自动化该过程,而不需要任何用户输入或任何用户提示(例如:在CI/CD管道中),您可以使用以下方法利用git凭据.helper

GIT_CREDS_PATH="/my/random/path/to/a/git/creds/file"
# Or you may choose to not specify GIT_CREDS_PATH at all.
# See https://git-scm.com/docs/git-credential-store#FILES for the defaults used

git config --global credential.helper "store --file ${GIT_CREDS_PATH}"
echo "https://alice:${ALICE_GITHUB_PASSWORD}@github.com" > ${GIT_CREDS_PATH}

在这里你可以选择设置ALICE_GITHUB_PASSWORD环境变量从以前的shell命令或从管道配置等。

记住,基于“存储”的git- credentials -helper以明文形式存储密码和值。因此,请确保您的令牌/密码的权限非常有限。


现在只需使用https://alice@github.com/my_repo.git无论你的自动系统需要获取回购-它将使用凭据alice在github.com存储由git-credential-helper。