2014-03-31 52 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

相關問題