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

2021-10-18 07:02:09 字數 2380 閱讀 6604

需求:

公司有gitlib賬號,

某個開源專案有github賬號,

自己有github賬號,

自己也有gitee賬號,

想在git上同時使用,

這麼多賬號如何做到 兩者互不干擾。

思路:

管理n個shh key,這裡就拿兩個舉個例子吧

解決方案:

一、生成兩個ssh key

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

在git 的命令列環境下執行:

cd ~/.ssh
在~/.ssh(windows下 就是c:\users\使用者名稱.ssh 比如我c:\users\admin.ssh) 路徑下執行命令列

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

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

每個命令執行完畢之後不要一路回車,在第乙個對話的時候輸入重新命名(id_rsa_oneid_rsa_two),這樣會生成

id_rsa_one

id_rsa_one.pub

id_rsa_two

id_rsa_two.pub

兩份包含私鑰和公鑰的4個檔案。

二、新增私鑰

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])

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賬號中。

id_rsa_one.pub

id_rsa_two.pub

五、遠端測試【可跳過】

$ 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的使用者名稱/learngit.git

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

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

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

參考:

在一台電腦上使用多個git賬號

步驟一 用ssh keygen命令生成一組新的id rsa new和id rsa new.pub。1 ssh keygen t rsa c new email 平時我們都是直接回車,預設生成id rsa和id rsa.pub。這裡特別需要注意,出現提示輸入檔名的時候要輸入與預設配置不一樣的檔名,比如...

一台電腦配置多個git

如果之前沒有設定過全域性配置可以跳過此步。如果多個git賬號郵箱 使用者名稱一樣也跳過此步。檢視當前是否有全域性配置 git config global user.name git config global user.email移出全域性使用者名稱和郵箱 git config global uns...

如何在一台電腦上使用多個git賬號

有時我們需要在一台電腦上使用多個git賬號,如乙個個人賬號使用者管理個人專案,乙個公司賬號用於工作。為了使用遠端倉庫,需要在本機生成id rsa和id rsa.pub,並把公鑰id rsa.pub作為ssh key新增到遠端倉庫。這一步想必大家使用過遠端倉庫的都用過,具體可以去廖雪峰老師的教程學習。...