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

但它们并没有起作用。


当前回答

根据Michael Scharf的评论:

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

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

它将提示您输入密码。

或者,你可以使用:

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

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

其他回答

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

如果您正在使用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。

git config --global core.askpass

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

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

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

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

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

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

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