github一把公鑰只能用於一個github帳戶,若是想在同一主機上給兩個屬於不一樣帳戶的倉庫提交時,必須在本地建立兩對公/私鑰匙,分別把兩把公鑰給兩個賬號。git
或者有時候,你公司內部使用的gitlab,同時你我的又有github,你想用同一個公鑰將倉庫分別提交到github和gitlab。github
生成第一把公鑰: ssh-keygen -t rsa -C "kobe@email.com" # 設置名稱爲id_rsa_kobe Enter file in which to save the key ((/home/xxxx/.ssh/id_rsa)): id_rsa_kobe #添加到SSH agent中 ssh-add id_rsa_kobe 製造第二把公鑰: ssh-keygen -t rsa -C "jordan@email.com" # 設置名稱爲id_rsa_jordan Enter file in which to save the key ((/home/xxxx/.ssh/id_rsa)): id_rsa_jordan #添加到SSH agent中 ssh-add id_rsa_jordan
# 在.ssh目錄下配置config文件: Host kobe HostName github.com User git IdentityFile ~/.ssh/id_rsa_kobe Host jordan HostName github.com User git IdentityFile ~/.ssh/id_rsa_jordan
ssh -T kobe Hi kobe! You've successfully authenticated, but GitHub does not provide shel l access. ssh -T ranpop Hi jordan! You've successfully authenticated, but GitHub does not provide shel l access.
對於kobe賬號下的倉庫: git clone kobe:githubname/repository.git (原地址是:git@github.com:githubname/repository.git,替換後應該是:kobe:githubname/repository.git) 對於ranpop賬號下的倉庫: git clone jordan::githubname/repository.git (原地址是:git@github.com:githubname/repository.git,替換後應該是:jordan:githubname/repository.git)
# 若是已經使用原地址克隆過了,能夠使用以下命令修改 git remote set-url origin kobe:githubname/repository.git # 若是是本地新建的倉庫,能夠使用以下命令添加 git remote add origin jordan:githubname/repository.git
# 在.ssh目錄下配置config文件: Host github HostName github.com User git IdentityFile ~/.ssh/id_rsa_kobe Host gitlab HostName gitlab.com User git IdentityFile ~/.ssh/id_rsa_kobe
ssh -T github Hi kobe! You've successfully authenticated, but GitHub does not provide shel l access. ssh -T gitlab Welcome to GitLab, @kobe!
git remote add github github:githubname/repository.git git remote add gitlab gitlab:githubname/repository.git
# 推送master分支到github git push github master # 推送master分支到gitlab git push gitlab master