Windows下配置多個Git帳號

1. 爲何會用多個git帳號? html

  • 不一樣git帳號對應不一樣代碼託管平臺,如:github、bitbucket、gitlab、gitee(碼雲)等
  • 2個GitHub帳號,用於測試(最近需求,以前沒研究過github協同開發,因此註冊了個小號來測試)

2. 不一樣git帳號對應不一樣代碼託管平臺,以github和bitbucket爲例 git

1)假設你已經生成了不一樣的 sshkey,其中針對不一樣的平臺能夠命名以下:github

  • github:id_github_rsa
  • bibucket:id_bitbucket_rsa
  • 注意:爲了管理方便,把id_github_rsa、id_github_rsa.pub 和 id_bitbucket_rsa、id_bitbucket_rsa.pub都移到同一目錄下

2)個人移動到 /c/Users/xxx/.ssh/目錄下,在該目錄下手動建立文件「config」(無後綴名),編輯config文件,以下:segmentfault

# github account [github_user]
Host github.com
HostName github.com
User github_user
IdentityFile /c/Users/xxx/.ssh/id_github_rsa
IdentitiesOnly yes

# bitbucket account [bct_user]
Host bitbucket.org
HostName bitbucket.org
User btc_user
IdentityFile /c/Users/xxx/.ssh/id_bitbucket_rsa
IdentitiesOnly yes

3)經過上述配置後,你就可使用多個git帳號操做對應的代碼託管平臺(假設你使用同一個email註冊不一樣的平臺,不然的話,須要取消全局email和username設置,而且在使用前經過git config命令切換帳號,具體請看 3. 同一個平臺GitHub下的不一樣git帳號bash

3. 同一個平臺(GitHub爲例)下的不一樣git帳號session

1)config 配置以下:ssh

# github account [user1]
Host github.com
HostName github.com
User user1
IdentityFile /c/Users/xxx/.ssh/id_user1_rsa
IdentitiesOnly yes

# github account [user2]
Host github.com
HostName github.com
User user2
IdentityFile /c/Users/xxx/.ssh/id_user2_rsa
IdentitiesOnly yes

2)在使用時須要注意,不能設置全局的 username 和 emailgitlab

# 取消全局 username, email
>git config --global --unset user.name
>git config --global --unset user.email

3)如要在repo中使用 user1 進行操做,進入repo目錄後,先設置username 和 email,再進行其餘操做測試

>git config user.name "user1"
>git config user.email "user1登錄GitHub的email"
# 一樣的,若是想在repo中切換爲 user2 進行操做,則從新設置username和email(同上)後,再進行其餘操做

4. 可能遇到的問題spa

1)git push:remote: Permission to XXXA/xxxx.git denied to XXXB

緣由:

  • 以前爲了測試GitHub的一些機制,我在同一個電腦上配置了兩個GitHub帳號:southday | lcxv
  • 最近在向southday帳號下的項目push代碼時,出現:remote: Permission to XXXA/xxxx.git denied to lcxv
  • 這是因爲電腦使用git bash配過SSH,系統已經將指向github.com的用戶設置爲了lcxv,每次push操做的時候,默認讀取保存在本地的用戶lcxv

解決方法:

  • 解決這個問題最簡單的辦法是刪除本機中GitHub的lcxv登錄session
  • 從新提交的時候,git會要求你輸入新的用戶名和密碼,輸入後就能夠成功提交了

5. 參考內容

轉載請說明出處!have a good time :D

相關文章
相關標籤/搜索