一臺機器git配置gitlab和github兩個賬號和郵箱

環境:

window10 64位
tortoisegit
git
gitlab

背景

由於公司使用gitlab部署私有代碼庫,個人有需求查看github的開源代碼,如果配置全局的賬號密碼會衝突。所以有必要生成兩個id_rsa.pub密鑰分別配置到github和gitlab的ssh密鑰中去。

步驟

  1. 下載git和tortoisegit

  2. 在空文件夾右鍵“Git Bash Here”,如圖所示,進去git bash界面。
    在這裏插入圖片描述

  3. 設置無密碼登錄
    a.生成第1個git無密碼登錄的id_rsa

    ssh-keygen -t rsa -f ~/.ssh/id_rsa_1 -C “第1個郵箱”

    如圖所示,直接回車即可
    無密碼登錄
    b.生成第2個git無密碼登錄的id_rsa

    ssh-keygen -t rsa -f ~/.ssh/id_rsa_2 -C “第2個郵箱”

  4. 此時能在~/.ssh/config文件中看到第一個郵箱的配置,添加第二個郵箱配置。

    vim ~/.ssh/config

     # 該文件用於配置私鑰對應的服務器
     # first user
     Host [email protected]
     HostName https://github.com
     User 用戶名
     IdentityFile ~/.ssh/id_1
     
     # second user
     Host [email protected]
     HostName http://gitlab.10101111.com
     User 用戶名
     IdentityFile ~/.ssh/id_rsa_2
    
  5. 將id_rsa_1.pub和id_rsa_2.pub分別配置到github.com和gitlab.10101111.com的ssh密鑰配置中。
    在github找到“Settings->SSH and GPG keys”添加id_rsa_1.pub;在gitlab.10101111.com找到“設置->SSH密鑰”添加id_rsa_2.pub。
    在這裏插入圖片描述

  6. 取消git的全局配置(如果之前就有配置的情況下)

    git config --global --unset user.name “XXX”
    git config --global --unset user.email "[email protected]"

    查看是否取消成功:

    git config --global -l

    如圖下圖所示,如果沒有用戶名和郵箱就代表取消了全局配置了。

  7. 執行ssh識別
    (1)#Start the ‘ssh-agent.exe’ process

    eval $(ssh-agent -s)

    (2)#install the SSH keys

    ssh-add ~/.ssh/id_rsa_1
    ssh-add ~/.ssh/id_rsa_2

    (3)# show all id_rsa

    ssh-add -l

    在這裏插入圖片描述
    注意:
    1、如果ssh-add這步報錯:Could not open a connection to your authentication agent.
    需要先啓動ssh-agent,也就是上面的第一個步驟。
    參考鏈接:https://stackoverflow.com/questions/17846529/could-not-open-a-connection-to-your-authentication-agent
    2、如果執行ssh-add -l報錯:Could not open a connection to your authentication agent.
    在這裏插入圖片描述
    需要先將密鑰添加,也就是上述的第二步。
    參考鏈接:https://stackoverflow.com/questions/26505980/github-permission-denied-ssh-add-agent-has-no-identities

  8. 測試是否能無密碼連接成功

    ssh -T [email protected]
    ssh -T [email protected]

    在這裏插入圖片描述

  9. 此時可以克隆代碼到本地了。

    git clone [email protected]:grcpeng/spring-boot.git

    在這裏插入圖片描述
    注意:(未經測試)
    有文章說需要配置不同項目下的用戶名和郵箱,在此做一個備註。
    需要進入到需要修改的git工程目錄中

    git config user.name 用戶名
    git config user.email 郵箱

參考鏈接:

1、https://blog.csdn.net/qq1332479771/article/details/70149616
2、https://blog.csdn.net/jifaliwo123/article/details/79126785
3、http://www.xuanfengge.com/using-ssh-key-link-github-photo-tour.html
4、https://gist.github.com/yeungeek/596984fd9e53d6c36c0d
5、https://favoorr.github.io/2015/05/27/git-more-sshkeys-more-host/

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