我知道如何为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
但它们并没有起作用。
要克隆存储库,首先应该生成一个访问令牌(您不能再使用登录密码),然后使用这个生成的令牌克隆存储库。
生成访问令牌:
github:
设置>开发人员设置>个人访问令牌>生成新的令牌
bitbucket都:
个人设置>应用密码>创建应用密码
克隆存储库:
github:
$ git clone https://YOUR-USERNAME:GENERATED-TOKEN@github.com/YOUR-USERNAME/YOUR-REPOSITORY
bitbucket都:
git clone https://YOUR-USERNAME@bitbucket.org/YOUR-REPOSITORY
它将提示您输入已生成的令牌。
如果您正在使用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。