一台電腦上的git同時使用兩個github賬戶

來源:互聯網
上載者:User

標籤:

 

需求:

公司有github帳號,自己有github帳號,想在git上同時使用,兩者互不干擾。

 

思路:

管理兩個SHH key。

 

解決方案:

 

一、產生兩個SSH key

 

為了舉例方便,這裡使用“one”和“two”兩個賬戶。下同。

 

$ ssh-keygen -t rsa -C "[email protected]"$ ssh-keygen -t rsa -C "[email protected]"

 

不要一路斷行符號,分別在第一個對話的時候輸入重新命名(id_rsa_oneid_rsa_two),這樣會產生兩份包含私密金鑰和公開金鑰的4個檔案。

 

注1:ssh-keygen是linux命令,可以讓兩個機器之間使用ssh而不需要使用者名稱和密碼

住2:一定要在~/.ssh路徑下運行命令列,不然產生的檔案不會出現在目前的目錄

 

二、添加私密金鑰

 

1、開啟ssh-agent


(1)如果你是github官方的bash:

$ ssh-agent -s

(2) 如果你是其它,比如msysgit:

$ eval $(ssh-agent -s)

 

2、添加私密金鑰

$ ssh-add ~/.ssh/id_rsa_one$ ssh-add ~/.ssh/id_rsa_two

 

三、建立config檔案

 

$ touch config

 

此時會出現空的config檔案,然後添加如下內容:

 

# one([email protected])    Host one.github.com  HostName github.com  PreferredAuthentications publickey  IdentityFile ~/.ssh/id_rsa_one  User one # two([email protected] gmail.com)    Host two.github.com  HostName github.com  PreferredAuthentications publickey  IdentityFile ~/.ssh/id_rsa_two  User two

 

四、部署SSH key

 

分別登陸兩個github帳號,進入Personal settings –> SSH and GPG keys

 

點擊"new SSH key", 把下面兩個公開金鑰的內容分別添加到相應的github帳號中。

  

五、遠程測試【可跳過】

 

$ ssh –T one.github.com$ ssh –T two.github.com

 

六、使用

 

1、clone到本地

 

(1)原來的寫法:

$ git clone [email protected]: one的使用者名稱/learngit.git

(2)現在的寫法:

$ git clone [email protected]one.github.com: one的使用者名稱/learngit.git$ git clone [email protected]two.github.com: two的使用者名稱/learngit.git

 

2、記得給這個倉庫設定局部的使用者名稱和郵箱:

$ git config user.name "one_name" ; git config user.email "one_email"$ git config user.name "two_name" ; git config user.email "two_email"

 

3、上述都成功後,會發現鑰匙會由灰變綠。

 

一台電腦上的git同時使用兩個github賬戶

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.