Windows配置多git帳號

1 前言

    若是咱們在公司有一個GitHub帳號,有一個私人的GitHub帳號,那平時在家工做時,就須要在電腦配置兩個GitHub帳號才行,上教程git

2 過程

    2-1 生成祕鑰github

        在某個文件夾打開gitbash窗口,執行下面命令:bash

# gh爲名稱,任意填寫
git config --global user.name gh
# test@xxx.com 是登陸github等網站時的郵箱
git config --global user.email test@xxx.com

        而後:ssh

# test@xxx.com 是上面輸入的郵箱
ssh-keygen -t rsa -C test@xxx.com

        結果以下圖:網站

        第一個爲輸入的執行命令,第二個 ‘first’ 是輸入的祕鑰名稱,不一樣帳號應對應不一樣名字,第三第四處紅框直接回車,執行完畢後會在當前文件夾下生成 first 和 first.pub 兩個文件:spa

    2-2 將祕鑰放入我的設置.net

        以github爲例,登陸 --> settings --> SSH and GPG keys --> New SSH keycode

        title: 隨便填
        key:  將 first.pub 祕鑰中的內容複製到裏面教程

    2-3 重複上面兩個命令,生成祕鑰,而後放入網站get

    2-4 新建並修改 config 文件

        在 C:\Users\Administrator\.ssh 文件夾下創建 config 文件:

#first account
Host first
HostName github.com
User first
IdentityFile ~/.ssh/first

#second account
Host second
HostName github.com
User second
IdentityFile ~/.ssh/second

其中,HOST: 任意,
          HostName:    github --> github.com      碼雲 --> git.oschina.net
          User: 隨意,我寫的是 2-1 中的 user.name

          IdentityFile: ~/.ssh/xxx    xxx是祕鑰文件中的文件名,即上面的 first

     2-5 clone 項目

          git clone git@first:用戶名/項目.git

          其實就是用 config 中的 Host 代替 Hostname, 至關於給域名起了個別名

    2-6 默認帳號         電腦配置了度帳號以後,最近在公司push代碼時用的老是私人帳號,發如今 C 盤我的用戶目錄下存在一個 .gitconfig 文件,能夠進行相關配置

相關文章
相關標籤/搜索