该user@host:path/to/repo格式告诉Git的使用SSH登录到host使用用户名user。来自git help clone:
ssh协议也可以使用类似scp的语法:
[user@]host.xz:path/to/repo.git/
之前的部分@是用户名,身份验证方法(密码,公钥等)由ssh而不是Git确定。Git无法将密码传递给ssh,因为ssh甚至可能不使用密码,具体取决于远程服务器的配置。
使用ssh-agent以避免键入密码的所有时间
如果您不想一直输入ssh密码,通常的解决方案是生成一个公钥/私钥对,将公钥放入远程服务器上的~/.ssh/authorized_keys文件中,然后将私钥加载到中ssh-agent。另请参阅通过SSH配置Git登录一次,有关ssh密钥密码的GitHub帮助页面,gitolite的ssh文档和Heroku的ssh密钥文档。
在GitHub(或Heroku或...)的多个帐户之间进行选择
如果您在GitHub或Heroku这样的地方有多个帐户,则将有多个ssh密钥(每个帐户至少一个)。要选择您要登录的帐户,必须告诉ssh使用哪个私钥。
例如,假设您有两个GitHub帐户: foo和bar。为您的SSH密钥foo就是~/.ssh/foo_github_id对你的SSH密钥bar是~/.ssh/bar_github_id。您想git@github.com:foo/foo.git使用您的foo帐户和git@github.com:bar/bar.git您的bar帐户进行访问。您可以将以下内容添加到您的~/.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的替代方法:
警告:将密码添加到克隆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详细信息,请参见。