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

其他回答

如果这些参数正在产生问题,请使用ur的特殊情况替换它们:

 !   #   $   &   '   (   )   *   +   ,   /   :   ;   =   ?   @   [   ]

%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://git.example.com/ns/repo.git --config=credential.helper=store

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

根据Michael Scharf的评论:

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

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

它将提示您输入密码。

或者,你可以使用:

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

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

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

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/

user@host:path/to/repo格式告诉Git使用ssh以用户名user登录主机。从git帮助克隆:

另一种类似scp的语法也可以用于ssh协议: [user@] host.xz: / / repo.git /路径

@前面的部分是用户名,身份验证方法(密码、公钥等)由ssh决定,而不是Git。Git无法将密码传递给ssh,因为ssh甚至可能不使用密码,这取决于远程服务器的配置。

使用ssh-agent可以避免一直输入密码

如果您不想一直输入ssh密码,典型的解决方案是生成一个公钥/私钥对,将公钥放在~/。打开远程服务器上的Ssh /authorized_keys文件,并将私钥加载到Ssh -agent中。还可以参考配置Git通过SSH登录一次,GitHub的SSH密钥密码帮助页面,gitolite的SSH文档和Heroku的SSH密钥文档。

在GitHub(或Heroku或…)

如果你在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

完全避免ssh

一些服务提供HTTP访问作为ssh的替代方案:

GitHub: https://username:password@github.com/username/repository.git Gitorious: https://username:password@gitorious.org/project/repository.git Heroku:请参阅此支持文章。

警告:将您的密码添加到克隆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帮助凭证。