平時本身會用到github來管理本身的一些項目,同時公司也在用gitlab作版本管理,那麼如何在一臺電腦上同時鏈接gitlab和github呢?下面展現一下配置的步驟node
ssh-keygen -t rsa -C "your.email@example.com" -b 4096
複製代碼
生成第一個gitlab的ssh key一路回車便可,生成第二個github的ssh key需注意一下ssh key的名字須要修改一下,不然就會覆蓋前面生成的gitlab的key,這裏我修改爲id_rsa_github git
- id_rsa
- id_rsa.pub
- id_rsa_github
- id_rsa_github.pub
複製代碼
cat ~/.ssh/id_rsa.pub
複製代碼
在.ssh/目錄下新建一個config文件github
vim config
複製代碼
添加配置,內容爲shell
# gitlab
Host gitlab
User git
HostName gitlab.cheanjiait.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa
# github
Host github
User git
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_github
複製代碼
$ ssh -T gitlab
Welcome to GitLab, Ethan Chen!
複製代碼
$ ssh -T github
Hi azumia! You've successfully authenticated, but GitHub does not provide shell access. 複製代碼
若是出現以上內容,則表明鏈接已經成功了,接下來就能夠愉快的搞git了vim
在使用github時,在項目下初始化git的時候記得定義好user.name和user.emailsegmentfault
git config --local user.name 'aaa'
git config --local user.email 'aaa@qq.com'
複製代碼
若是測試鏈接失敗,Permission denied (publickey).緣由是們自定義了 id_rsa_github 鑰匙名,默認狀況下,鏈接會搜索 id_rsa 鑰匙名,因此這裏會失敗bash
能夠經過如下操做了解鏈接失敗的具體緣由ssh
ssh -T -v git@github.com
複製代碼
針對這個問題的解決方案以下ide
# 開啓 agent
eval $(ssh-agent -s) ←┘
Agent pid 8428
複製代碼
# 添加 鑰匙名
ssh-add ~/.ssh/id_rsa_github ←┘
Identity added: /c/Users/user/.ssh/id_rsa_github (/c/Users/user/.ssh/id_rsa_github)
複製代碼
# 查看 agent list
ssh-add -l ←┘
8428 SHA256:A9UcJMadwTpF7TvsT5LEXsSssTs5qehmV9Q2Q8Ntw3o /c/Users/user/.ssh/id_rsa_github (RSA)
複製代碼
# 不用時能夠關閉 agent
eval $(ssh-agent -k) ←┘
Agent pid 8428 killed
複製代碼
若是初始化倉庫的時候報如下錯誤gitlab
ssh: Could not resolve hostname https: nodename nor servname provided, or not known
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
複製代碼
則查看一下本身的git的配置
$ git remote -v
複製代碼
將git地址由ssh方式改成https方式便可
參考文檔: 同時使用:gitlab & github