git ssh 配置多個賬戶的方法
一臺電腦上有一個ssh key,在github上提交代碼,由于其他原因
你可能會在一臺電腦上提交到不同的github上,怎么辦呢…
所以查了一下關于同機器多賬號的ssh-key配置,在此記錄一下。
操作步驟
如果我們Windows 系統(tǒng)上面已經(jīng)有了ssh-key再創(chuàng)建ssh-key的話,需要給我們的ssh-key文件取不同的名字,默認是id_rsa,如果不重新起名的話,會把原有的給覆蓋掉。
1.新建ssh-key&重新命名
這里我們新建的key名為 id_rsa2,路徑為:C:\Users\Administrator.ssh
首先打開git bash ,可以通過在桌面上右鍵打開 (右鍵 》 git bash here),切換到ssh目錄,輸入一下命令
//切換到ssh目錄 cd ~/.ssh
//新建ssh-key ssh-keygen -t rsa -C "mywork@email.com" //為新建的ssh-key重新命名 Enter file in which to save the key
輸入文件名及路徑
C:\Users\Administrator\.ssh\id_rsa2
這樣的話會在 目錄下生成兩個文件
C:\Users\Administrator\.ssh\id_rsa2 C:\Users\Administrator\.ssh\id_rsa2.pub
2.新ssh-key添加到ssh agent中
因為默認只讀取id_rsa,為了讓SSH識別新的私鑰,需將其添加到SSH agent中:
首先打開git Bash
//打開ssh-agent 客戶端,否則會報錯 eval `ssh-agent -s` ssh-add ~/.ssh/id_rsa2
3.配置 將不同賬號的工程圖服務器與ssh-key關聯(lián)
賬戶以及git服務器與對應的密鑰關聯(lián)。在~/.ssh目錄下找到config文件,如果沒有就創(chuàng)建:
touch config
然后修改如下: 我的config配置如下:
# 該文件用于配置私鑰對應的服務器 # Default github user(first@mail.com) Host github.com HostName github.com User git IdentityFile C:/Users/Administrator/.ssh/id_rsa # second user(second@mail.com) # 建一個github別名,新建的帳號使用這個別名做克隆和更新 Host github2 HostName github.com User git IdentityFile C:\Users\Administrator\.ssh\id_rsa2
其規(guī)則就是:從上至下讀取config的內容,在每個Host下尋找對應的私鑰。這里將GitHub SSH倉庫地址中的git@github.com替換成新建的Host別名如:github2,那么原地址是:git@github.com:funpeng/Mywork.git,替換后應該是:github2:funpeng/Mywork.git.
4、用記事本打開新生成的~/.ssh/id_rsa2.pub文件,將里面的內容添加到GitHub后臺。
可不要忘了添加到你的另一個github帳號下的SSH Key中。
5、測試:
$ ssh -T git@github.com
Hi gdutxiaoxu! You've successfully authenticated, but GitHub does not provide shell access..
$ ssh -T github2
Hi xujun94! You've successfully authenticated, but GitHub does not provide shell access.
6、應用
假如我們要克隆我們的遠程倉庫:https://github.com/xujun94/xujun.git ,在我的github 賬戶上配置 的為 ssh 可以為 id_isa2,正常情況下我們是需要輸入命令
git clone git@github.com:xujun94/xujun.git
git clone github2:xujun94/xujun.git
進入我們的倉庫 ,添加xujun.txt 文件,add > commit > push ,將可以看到如下的效果
如果你只是通過這篇文章中所述配置了Host,那么你多個賬號下面的提交用戶會是一個人,所以需要通過命令git config –global –unset user.email刪除用戶賬戶設置,在每一個repo下面使用git config –local user.email ‘你的github郵箱@mail.com' 命令單獨設置用戶賬戶信息
相關文章
在 WordPress 的頁眉(header)和頁腳(footer)添加代碼方法
這篇文章主要介紹了在 WordPress 的頁眉(header)和頁腳(footer)添加代碼方法2021-09-09重裝win10系統(tǒng)超詳細的圖文教程(適用所有windows系統(tǒng))
這篇文章主要介紹了重裝win10系統(tǒng)超詳細的圖文教程(適用所有windows系統(tǒng)),非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-11-11