搞定Git多賬戶配置,一臺電腦多個Git賬戶

在日常開發或工作中,我們不免會使用到多個git平臺的賬號如:github、gitee、gitlab,下面介紹如何在一臺機器上使用多個git賬號

  1. 生成SSH祕鑰

    ssh-keygen -o -t rsa -b 4096 -C "[email protected]"
    

    這條命令會在用戶目錄下的.ssh/目錄生成一個名爲id_rsaid_rsa.pub的文件,單個git平臺賬戶使用時是沒什麼問題的,但是如果有多個git賬戶就需要在這裏指定一個生成的公鑰文件名了。這裏拿github和gitee舉例:

    ssh-keygen -o -t rsa -b 4096 -C "你的gitee註冊郵箱" -f ~/.ssh/id_rsa_gitee
    ssh-keygen -o -t rsa -b 4096 -C "你的github註冊郵箱" -f ~/.ssh/id_rsa_github
    

    輸出一下內容說明公鑰生成成功:

    在這裏插入圖片描述

    然後把公鑰文件內容 粘貼到對應的平臺的SSH公鑰上:

    複製Gitee的公鑰內容

    cat ~/.ssh/id_rsa_gitee.pub | clip
    

    複製Github的公鑰內容

       cat ~/.ssh/id_rsa_github.pub | clip
    
  2. 配置config文件 指定公鑰和git平臺的映射關係

    ~/.ssh目錄下增加一個文本文件config(沒有文件後綴)

    文件內容 輸入時請注意縮進:

    Host github.com	#平臺地址
        HostName github.com	#平臺地址
        IdentityFile C:\\Users\\yangxh\\.ssh\\id_rsa-github	# 公鑰文件路徑
        PreferredAuthentications publickey
    	User feasy-code	#平臺用戶名
    Host gitee.com	#平臺地址
        HostName gitee.com	#平臺地址
        IdentityFile C:\\Users\\yangxh\\.ssh\\id_rsa-gitee # 公鑰文件路徑
        PreferredAuthentications publickey
    	User yangxiaohui	#平臺用戶名
    

    Host #平臺地址

    HostName github.com #平臺地址

    IdentityFile # 公鑰文件路徑

    User #平臺用戶名

  3. 驗證配置

    ssh -T [email protected]
    

    在這裏插入圖片描述

       ssh -T [email protected]
    

    在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章