在推送GitHub期间从命令行使用SSH密钥?

时间:2015-07-20 15:14:35

标签: authentication github ssh

我在GitHub上存在SSH密钥。我试图将改变推向别人的GitHub项目。我有 Collaborator 访问该帐户。

我被提示输入密码:

$ git push
warning: push.default is unset; its implicit value is changing in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the current behavior after the default changes, use:

  git config --global push.default matching

To squelch this message and adopt the new behavior now, use:

  git config --global push.default simple

When push.default is set to 'matching', git will push local branches
to the remote branches that already exist with the same name.

In Git 2.0, Git will default to the more conservative 'simple'
behavior, which only pushes the current branch to the corresponding
remote branch that 'git pull' uses to update the current branch.

See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)

Username for 'https://github.com': xxxxxx
Password for 'https://xxxxxx@github.com': 
remote: Invalid username or password.
fatal: Authentication failed for 'https://github.com/weidai11/cryptopp.git/'

GitHub在Which remote URL should I use?的帮助仅在桌面客户端的上下文中讨论了SSH。我猜他们有一个他们希望我使用的应用程序,但我真的不想使用它。

如何让命令行工具使用我的SSH密钥将更改推送到GitHub帐户?

这与How do I checkout a GitHub project from command line as a Collaborator?有关。作为合作者,我从来没有能够以凭证结账,现在我陷入了这个密码认证陷阱。

1 个答案:

答案 0 :(得分:1)

要推送另一个帐户,您需要存储在~/.ssh文件夹中的其他帐户的私人/公共ssh密钥。

您还需要一个类似于:

的ssh配置文件(~/.ssh/config

ssh配置文件声明多个键:

Host githubUserA
    Hostname github.com
    IdentityFile ~/.ssh/id_rsa
    User git

Host githubUseB
    Hostname github.com
    IdentityFile ~/.ssh/other
    User git

你会使用像

这样的ssh网址
githubUserA:userA/myrepo1
githubUserB:userB/myrepo2

可以通过添加新的遥控器从同一个仓库中使用第二个帐户:

git remote add guserB githubUserB:userB/myrepo2
git push gUserB myBranch