window下git多賬戶管理


前言

一般情況下,我們都是一台電腦配置一個Git賬號,使用如下命令:

git config --global user.name "your name"
git config --global user.email "your email"

我的電腦上已經配置GitHub的,現需要配置公司的 GitLab 賬號(或者其他類型的 Git 賬號)。

配置多個git賬號

清除原有全局設置

該步驟非必須的,如果你沒有設置過全局用戶名、email 等信息,則不用清除原有設置

  • 取消全局設置
git config --global --unset user.name
git config --global --unset user.email

生成ssh密鑰

  • 生成github 對應賬號的ssh密鑰
ssh-keygen -t rsa -C "your_github_email@xxx.com"
  • 執行完上面的命令后,命令行會彈出以下提示
Enter file in which to save the key (/c/Users/admin/.ssh/id_rsa):
  • 在此處修改id_rsa文件名(注意:不要覆蓋之前的rsa密鑰)
/c/Users/admin/.ssh/id_rsa_test
  • 之后就一直回車即可
  • 文件夾ssh下就生成了 id_rsa_test 和id_rsa_test.pub
  • 最后將id_rsa_test.pub里的內容粘貼到github服務器的指定位置上

以上只是一個賬號ssh的生成過程,其他賬號同上

修改config文件(如果沒有就在.ssh 文件夾下創建一個)

# The git info for githunb
Host test                                            #服務器別名
HostName github.com                                  #主機地址
User git                                             #可以寫郵箱名稱,也可以寫 git 賬號
IdentityFile C:\\Users\\admin\\.ssh\\id_rsa_test     #對應github 密鑰路徑,注意不要寫錯
IdentitiesOnly yes                                   #配置yes,表示只使用這里的key,防止使用默認的(可忽略)
PreferredAuthentications publickey                   #強制驗證方式,這里使用的是publickey 

# The git info for company
Host company                                         #服務器別名
HostName  gitLab.com                                 #公司gitLab 地址
User git                                             #可以寫郵箱名稱,也可以寫 git 賬號
IdentityFile C:\\Users\\admin\\.ssh\\id_rsa_company  #對應公司 gitLab 賬號密鑰路徑,注意不要寫錯
IdentitiesOnly yes
PreferredAuthentications publickey 

測試

  • 可以利用ssh -T git@test,測試github賬號是否成功。
    第一次執行會彈出
  • 這樣就配置完成了

使用

  • git clone
git clone git@test:worker/test.git
  • 通過config ,指定不同的 git 賬號對應不同的 ssh key
#全局設置賬號
git config --global user.name "your name"
git config --global user.email "your email"

#在項目中設置賬號--->需先進入到clone下的項目文件下
git config --local user.name "your name"
git config --local user.email "your email"


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM