git clone https://username:password@host
但是我想知道如何向SSH远程提供用户名和密码,就像这样:
git clone git@host.git
我试过这样做:
git clone username:password@git@host.git
git clone git@username:password@host.git
git clone git@host.git@username:password
但是他们没有起作用。
git clone https://username:password@host
git clone git@host.git
git clone username:password@git@host.git
git clone git@username:password@host.git
git clone git@host.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存储库,对我很有效。
git clone https://username@github.com/username/repository.git
。Git 会询问您,但密码不会保存在 .git/config
或您的 bash 历史记录中。 - Michael_Scharfgit clone https://<token>@github.com/username/repository.git
- ploscouser@host:path/to/repo
格式告诉Git使用ssh登录到host
,用户名为user
。从git help clone
中得知:
使用类似scp的语法也可以与ssh协议一起使用:
[user@]host.xz:path/to/repo.git/
@
符号之前的部分是用户名,验证方式(密码、公钥等)由ssh决定,而不是Git。 Git无法将密码传递给ssh,因为根据远程服务器的配置,ssh甚至可能不使用密码。
ssh-agent
避免频繁输入密码如果您不想频繁输入ssh密码,则通常解决方案是生成公钥/私钥对,将公钥放在远程服务器上的~/.ssh/authorized_keys
文件中,并将私钥加载到ssh-agent
中。此外,请参见在SSH上配置Git以一次登录、GitHub关于ssh密钥密码的帮助页面、gitolite的ssh文档和Heroku的ssh密钥文档。
如果您在类似GitHub或Heroku的地方有多个帐户,则会有多个ssh密钥(每个帐户至少一个)。要选择要作为哪个帐户登录,您必须告诉ssh使用哪个私钥。
例如,假设您有两个GitHub帐户:foo
和bar
。您的foo
帐户的ssh密钥为~/.ssh/foo_github_id
,而bar
帐户的ssh密钥为~/.ssh/bar_github_id
。您想使用foo
帐户访问git@github.com:foo/foo.git
,使用bar
帐户访问git@github.com:bar/bar.git
。您需要将以下内容添加到~/.ssh/config
:
Host gh-foo
Hostname github.com
User git
IdentityFile ~/.ssh/foo_github_id
Host gh-bar
Hostname github.com
User git
IdentityFile ~/.ssh/bar_github_id
您可以按照以下方式克隆这两个存储库:
git clone gh-foo:foo/foo.git # logs in with account foo
git clone gh-bar:bar/bar.git # logs in with account bar
有些服务提供HTTP访问作为ssh的替代方式:
GitHub:
https://username:password@github.com/username/repository.git
Gitorious:
https://username:password@gitorious.org/project/repository.git
Heroku: 请参考这篇支持文章。
WARNING: 将密码添加到克隆URL中会导致Git将明文密码存储在.git/config
文件中。当使用HTTP时,为了安全地存储您的密码,请使用凭据助手。例如:
git config --global credential.helper cache
git config --global credential.https://github.com.username foo
git clone https://github.com/foo/repository.git
上述操作会使Git每15分钟(默认设置)要求您输入密码。详情请参见git help credentials
。
如果您的特殊情况造成问题,请按照以下步骤进行替换:
! # $ & ' ( ) * + , / : ; = ? @ [ ]
%21 %23 %24 %26 %27 %28 %29 %2A %2B %2C %2F %3A %3B %3D %3F %40 %5B %5D
例如,实际URL为:https://usern@me:p@ssword@git/reponame.git
。使用的解决方案URL为:https://usern%40me:p%40ssword@git/reponame.git
。git clone https://<token>@github.com/username/repository.git
至少从GitHub克隆。我想我会详细说明一下如何做到这一点,以防有人像我一样在尝试自动化一些克隆时遇到这个答案。.git/config
中。对于几乎每种用例,这显然都是一种安全风险,但由于我计划在完成后删除repo并撤销令牌,所以我不在意。
GitHub有一个完整的指南, 介绍了如何获取令牌,但以下是TL;DR。
与 @plosco 给出的命令相同,git clone https://<token>@github.com/<username>/<repository>.git
,只需用你的信息替换 <token>
、<username>
和 <repository>
。
如果你想将其克隆到特定文件夹中,只需在末尾插入文件夹地址,如下所示:git clone https://<token>@github.com/<username>/<repository.git> <folder>
,其中 <folder>
是要克隆到的文件夹!当然,你可以像其他地方一样在这里使用 .
、..
、~
等。
根据您所做的事情的敏感程度,这些步骤可能并非全部必要。
rm -rf <folder>
。git remote remove origin
或者通过运行git remote set-url origin https://github.com/<username>/<repository.git>
来删除令牌。请注意,我不是专业人士,因此以上步骤可能在没有任何取证工作的意义上并不安全。
https://<token>@github.com
was not accepted with my portal, I had to use https://oauth2:<token>@github.com
- MushyPeas<token>
对于我使用个人访问令牌运行良好。 - Taylor D. Edmiston2021年8月13日后,你必须使用以下方式进行Git Clone:
Git Clone
https://username:token@github.com/username/repository.git
生成token的方式如下:
设置 >> 开发者设置 >> 个人访问令牌 >> 生成新令牌
成功克隆后,下次使用git push
时无需再提及用户名。您可以通过在记事本中打开.git/config
文件来确认此操作。
[remote "origin"]
url = https://username:tokenxxxxxxxxxx@github.com/username/repo.git
虽然有很多答案,但我自己在用户名或密码中存在特殊字符时会遇到重复的问题。
对于git,对您的用户名和密码进行URL编码,然后将其用作URL本身的一部分(当不存在安全问题时)。
例如,用户名的URL编码值为
'user+1' 是 user%2B1
以及密码的URL编码值为
'Welcome@1234' 是 Welcome%401234
那么您的GIT Clone URL将如下所示:
git clone https://user%2B1:Welcome%401234@actual-git-url-for-the-repo
很完美,而git clone https://user+1:Welcome@1234@actual-git-url-for-the-repo 将给您带来403错误
希望这有所帮助。
如果需要在线URL编码,请访问: https://www.urlencoder.org/
要克隆一个仓库,首先需要生成一个访问令牌(不能再使用您的登录密码),然后使用此生成的令牌克隆仓库。
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 credential.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}
您可以选择从先前的shell命令或管道配置中设置ALICE_GITHUB_PASSWORD
环境变量。
请记住,“存储”为基础的git凭据助手会以明文形式存储密码和值。因此,请确保您的令牌/密码具有非常有限的权限。
现在只需在自动化系统需要获取存储库的任何位置使用https://alice@github.com/my_repo.git,它将使用在git凭证助手中存储的 alice
在github.com
上的凭据。