思路
ssh 方式鏈接到 Github列牺,需要唯一的公鑰涉兽,如果想同一臺電腦綁定兩個Github 帳號嗤谚,需要兩個條件:
- 能夠生成兩對 私鑰/公鑰
- push 時,可以區(qū)分兩個賬戶藏畅,推送到相應(yīng)的倉庫
解決方案:
- 生成 私鑰/公鑰 時敷硅,密鑰文件命名避免重復(fù)
- 設(shè)置不同 Host 對應(yīng)同一 HostName 但密鑰不同
- 取消 git 全局
用戶名/郵箱
設(shè)置功咒,為每個倉庫獨(dú)立設(shè)置 用戶名/郵箱
操作方法
- 查看已有
密鑰
- Mac 下輸入命令
ls ~/.ssh/
,看到id_rsa
與id_rsa_pub
則說明已經(jīng)有一對密鑰绞蹦。
- 生成新的公鑰力奋,并命名為
id_rsa_2
(保證與之前密鑰文件名稱不同即可)
ssh-keygen -t rsa -f ~/.ssh/id_rsa_2 -C "yourmail@xxx.com"
- 在
.ssh
文件夾下新建config
文件并編輯,另不同 Host 實際映射到同一HostName
幽七,但密鑰文件不同景殷。Host 前綴可自定義,例子中ieit
# default
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa
# two
Host ieit.github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_2
- 將生成的
id_rsa.pub
澡屡,id_rsa_2.pub
內(nèi)容copy 到對應(yīng)的 repo
- 參考教程: 使用SSH密鑰連接Github【圖文教程】
- 測試 ssh 鏈接
ssh -T git@ieit.github.com
ssh -T git@github.com
# Hi IEIT! You've successfully authenticated, but GitHub does not provide shell access.
# 出現(xiàn)上邊這句猿挚,表示鏈接成功
- 將項目
clone
到本地,folder-name
是本地文件夾路徑
git clone git@github.com:whatever folder-name
- 取消全局 用戶名/郵箱設(shè)置驶鹉,并進(jìn)入項目文件夾單獨(dú)設(shè)置
# 取消全局 用戶名/郵箱 配置
git config –global –unset user.name
git config –global –unset user.email
# 單獨(dú)設(shè)置每個repo 用戶名/郵箱
git config user.email “xxxx@xx.com”
git config user.name “xxxx”
- 命令行進(jìn)入項目目錄绩蜻,重建 origin (whatever 為相應(yīng)項目地址)
git remote rm origin
git remote add origin git@ieit.github.com:whatever
- 成功,可以 push 測試一下
git push origin master