2014-03-31 50 views
9

我使用git下两种情况:如何为每个git存储库管理一个唯一的密钥?

  • 我使用一些Github上库。
  • 我目前正在使用OpenShift,它使用sshgit进行部署。

首先,我使用ssh-keygen来生成在OpenShift站点更新的密钥。此密钥存储在~/.ssh/并创建为id_rsaid_rsa.pub

然后我开始从Github克隆一个仓库,我再次做了ssh-keygen并开始推送,它工作正常。然后我克隆了另一个存储库并开始出现问题:

克隆到第二个存储库时出现问题。每次我试图推动会显示类似:

ERROR: Permission to diegoaguilar/cursoJava.git denied to diegoaguilar/cursoCannibalCreatures. fatal: The remote end hung up unexpectedly

但是,因为它可以看出diegoaguilar/cursoCannibalCreatures是不正确的,因为它是另一库。

我甚至尝试删除这样的存储库目录,并再次克隆它,也是如此。

我已经得到了~/.ssh下:

config

Host cursoJava 
Hostname github.com 
User git 
IdentityFile ~/.ssh/id_java 

Host cursoCannibalCreatures 
Hostname github.com 
User git 
IdentityFile ~/.ssh/id_cannibal 

Host openshift 
Hostname openshift.com 
User git 
IdentityFile ~/.ssh/openshift 

所以有:

id_cannibal id_cannibal.pub id_java id_java.pub known_hosts 

喜欢的东西id_openshiftid_openshift.pub是不存在的,但因为它不是工作,我现在不太在意。

我创建了这样的文件,他们.pubssh-keygen -f <filename>并给每个不同的密码短语。我在每个Github存储库设置中将.pub的内容添加为部署密钥。

我在做什么错?这应该如何工作?而且,在另一台机器上工作时,如何正确获取这些密钥,证明它是我的并透明地工作?

EDIT输出 git remote -v

  • 对于cursoJava库

origin [email protected]:diegoaguilar/cursoJava.git (fetch) origin [email protected]:diegoaguilar/cursoJava.git (push)

  • 对于cursoCannibalCreatures

origin [email protected]:diegoaguilar/cursoCannibalCreatures.git (fetch) origin [email protected]:diegoaguilar/cursoCannibalCreatures.git (push)

+0

你的公共ssh密钥在你的GitHub仓库管理页面中正确注册了正确的Github仓库吗? – VonC

+0

是的,他们,我仔细检查。我添加了所有我试过的东西,@VonC – diegoaguilar

+0

你可以复制'git remote -v'和'git branch -avvv'的结果,以及你输入的确切的git push命令,以及它的确切输出吗? – VonC

回答

24

正如在“ssh,github,it doesnot work”所提到的,关键是不使用默认的id_rsa(的.pub)为您的公开名称:私钥(因为哟只能定义一个夫妇的),但不同的名字。

但是,如果你要访问这些回购为不同的用户

在你的情况,你正在访问的回购与相同的用户这将是唯一的,并且一个SSH密钥应该足够。

请参阅 “GitHub help”:

This error means the key you are pushing with is attached to another repository as a deploy key, and does not have access to the repository you are trying to push to.

To remedy this, remove the deploy key from the repository, and attach the key to your user account instead.


这是使用GitHub上的两个不同的用户。

即可定义中,你通过自己的完整路径引用每个私钥一个~/.ssh/config文件:

Host github1 
    HostName github.com 
    User git 
    IdentityFile ~/.ssh/id_repo1 

Host github2 
    HostName github.com 
    User git 
    IdentityFile ~/.ssh/id_repo2 

使用[email protected]:user/repo1的相反,你可以使用:

github1:user/repo1 

使用的关键Host条目'github1'来引用用户(git),主机名(github.com)和确切的私钥/公钥使用~/.ssh/id_repo1(.pub)


所以,如果你有使用存储为~/.ssh/id_repo2(.pub)第二密钥的第二回购,你需要使用“github2”(只要你想,你可以将其命名)中所定义的条目,然后将URL你有产地:

git remote set-url origin github2:user/repo2 

这样一来,一个git push会使用正确的键(一个用于repo2

如果你不这样做,你将能够推动一个回购(使用默认密钥~/.ssh/id_rsa(.pub),默认为nam e),但您将无法推到第二个需要不同公钥/私钥集的回购协议。

+0

它的工作与失败在另一个github文件夹我得到,但不是在我有问题的人。当我尝试'git push'时,出现同样的错误 – diegoaguilar

+0

在这种情况下,这两个存储库都是同一个用户,这将如何更改配置文件,因此'git remote'命令? – diegoaguilar

+0

@diegoaguilar我没有意识到这是针对同一用户在这两种情况下。我编辑了我的答案。 – VonC

相关问题