user@host:path/to/repo 格式告诉 Git 使用 ssh 使用用户名 user 登录到 host。来自git help clone:
另一种类似 scp 的语法也可以与 ssh 协议一起使用:
[user@]host.xz:path/to/repo.git/
@之前的部分是用户名,认证方式(密码、公钥等)由ssh决定,而不是Git。 Git 无法将密码传递给 ssh,因为 ssh 甚至可能不使用密码,具体取决于远程服务器的配置。
使用ssh-agent 避免一直输入密码
如果你不想一直输入你的ssh密码,典型的解决方案是generate a public/private key pair,把你的公钥放在远程服务器上的~/.ssh/authorized_keys file,然后把你的私钥加载到ssh-agent .另请参阅Configuring Git over SSH to login once、GitHub's help page on ssh key passphrases、gitolite's ssh documentation 和 Heroku's ssh keys documentation。
在 GitHub(或 Heroku 或...)的多个帐户之间进行选择
如果您在 GitHub 或 Heroku 等地方拥有多个帐户,则您将拥有多个 ssh 密钥(每个帐户至少一个)。要选择您要登录的帐户,您必须tell ssh which private key to use。
例如,假设您有两个 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
-
霸道:
https://username:password@gitorious.org/project/repository.git
Heroku:见this support article。
警告:将您的密码添加到克隆 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。