git配置多用戶多平台


在Git使用中經常會碰到多用戶問題,例如:你在公司里有一個git賬戶,在github上有一個賬戶,並且你想在一台電腦上同時對這兩個git賬戶進行操作,此時就需要進行git多用戶配置。
首先配置不同的SSH KEY,使用ssh-keygen命令產生兩個不同的SSH KEY,進入.ssh目錄:

#切換到.ssh目錄
cd ~/.ssh  
#使用自己的企業郵箱產生SSH KEY
ssh-keygen -t rsa -C "mywork@email.com"  
#企業的可以使用id_rsa,也可以自己起名,例如:id_rsa_work
Enter file in which to save the key (/root/.ssh/id_rsa): id_rsa
#將ssh key添加到SSH agent中,該命令如果報錯:Could not open a connection to your authentication agent.無法連接到ssh agent,可執行ssh-agent bash命令后再執行ssh-add命令。
ssh-add ~/.ssh/id_rsa 

同理,配置自己的github賬戶,再有其他賬戶類似:

#切換到.ssh目錄
cd ~/.ssh  
#使用自己github的注冊郵箱產生SSH KEY
ssh-keygen -t rsa -C "mygithub@email.com"  
#github的SSH KEY
Enter file in which to save the key (/root/.ssh/id_rsa): id_rsa_github
#將ssh key添加到SSH agent中 ,該命令如果報錯:Could not open a connection to your authentication agent.無法連接到ssh agent,可執行ssh-agent bash命令后再執行ssh-add命令。
ssh-add ~/.ssh/id_rsa_github

在生成ssh key之后,需要分別在github的profile中和公司git的profile中編輯SSH KEY,以github為例:

1. Title,可以隨便寫: 
2. 將.ssh目錄下對應的id_rsa_github.pub中的內容拷到Key中,點擊Add SSH key按鈕即可。公司的git類似。 

然后在.ssh目錄下配置config文件:

#切換到.ssh目錄
cd ~/.ssh
#創建並編輯config文件
vim config 
# 粘貼到config文件中
#公司的git地址
Host git.***.com  
   User git
   Hostname git.***.com  #公司的git地址
   IdentityFile ~/.ssh/id_rsa  #訪問公司git的SSH KEY
   Port   ***  #公司的git端口

Host github.com
   User git
   Hostname github.com #github的地址
   IdentityFile ~/.ssh/id_rsa_github  #訪問github的SSH KEY

測試配置是否成功

#github的地址
ssh -T git@github.com 
#出現如下內容,表示成功鏈接github,***為你的github賬戶的用戶名
Hi ***! You've successfully authenticated, but GitHub does not provide shell access.
#公司的git地址
ssh -T git@git.***.com 
#出現如下內容,表示成功鏈接github,***為公司git賬戶的用戶名
Hi ***! You've successfully authenticated, but GitHub does not provide shell access.

提交代碼

本地如果沒有項目,git上創建項目倉庫並拉取:

git clone git@***:***/git-test.git
cd git-test
touch README.md
git add README.md
git commit -m "add README"
git push -u origin master

本地已有項目,git上創建倉庫並拉取

cd 已有項目目錄
git init
git remote add origin git@***:***/git-test.git
git add .
git commit
git push -u origin master


免責聲明!

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



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