如何在同一台電腦上使用兩個github賬戶(親測有效)


1 前言

由於有兩個github賬號,要在同一台電腦上同步代碼,需要給每一個賬號添加一個SSH public key,此時推送時git push origin,不知道是哪個賬號的遠程倉庫名稱,所以需要配置一下~/.ssh下面的config信息,否則會自動使用.ssh/id_rsa.pub所對應的賬戶進行登陸並操作。

若沒配置,提示的錯誤信息如下:

$ git push
ERROR: Permission to B/test2.git denied to A.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.  

2 步驟

解決方案(假設你已經擁有私有賬號且已經OK,現在想使用另一個工作用賬號):

2.1 為工作賬號生成SSH Key(先切換到 ~/.ssh下),具體參照Gitlab的SSH配置

ssh-keygen -t rsa -C your.email@example.com -b 4096                                                                                                                                                                                          

存儲key的時候,不要覆蓋現有的id_rsa,使用一個新的名字,比如id_rsa_work

2.2 配置.ssh/config

$ vi .ssh/config

# 加上以下內容
#default github
Host github.com
HostName github.com
IdentityFile ~/.ssh/id_rsa

Host github_work
HostName github.com
IdentityFile ~/.ssh/id_rsa_work

2.3 git push remote add origin2  

#push到github上去
$ git remote add origin2 git@github_work:B/test2.git

2.4 id_rsa_work.pub加到你的work賬號上

$ git remote -v
origin  git@github.com:B/test2.git (fetch)
origin  git@github.com:B/test2.git (push)
origin2 git@github_work:B/test2.git (fetch)
origin2 git@github_work:B/test2.git (push)

這樣就完成配置了。

2.5 git push origin2

git push origin2 master 

你就可以通過使用github.com別名github_work來明確說你要是使用id_rsa_work的SSH key來連接github,即使用工作賬號進行操作。


3 參考:

1. 如何在同一台電腦上使用兩個github賬戶


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM