我知道如何为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
但它们并没有起作用。
如果您正在使用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。
这是一个很棒的Stack Overflow问题,答案非常有启发性,因此我能够解决最近遇到的一个烦人的问题。
我工作的组织使用Atlassian的BitBucket产品(不是Github),本质上是他们的Github版本,因此存储库可以完全在前提下得到保护。我遇到了与@coordinate类似的问题,因为我签出的新存储库需要密码。我的证书被保存在所有BitBucket项目的全球范围内,所以我不确定是什么原因导致了证书的丢失。
简而言之,我可以输入以下GIT命令(只提供我的用户名),然后提示GIT的凭证管理器提示我输入密码,然后我就可以保存密码了。
git clone https://user@code.domain.org/git/[organization]/[team]/[repository.git]
注意:括号内的目录子路径只是指内部引用,并且会因你而异!