一台電腦,兩個及多個git賬號配置


1. 生成兩[三]個ssh公鑰私鑰

方法參照:http://www.cnblogs.com/fanbi/p/7772812.html第三步驟

假定其中一個是id_rsa, 另一個時id_rsa_two [,第三個是id_rsa_gitlab]。

2. 復制公鑰

把生成好的ssh的公鑰相對應復制到github[/gitlab]的settings中的SSH and GPG keys

3. 配置config文件

如果在~/.ssh/下沒有config文件,可以touch config

   ##可缺省,此時ssh -T git@github.com,默認就是和擁有id_rsa.pub的github賬號對接。
   #github server one
   Host github              #域名地址的別名
   Hostname github.com      #這個是真實的域名地址
   User git                     #配置使用用戶名
   IdentityFile ~/.ssh/id_rsa   #這里是id_rsa的地址
​
   #github server two
   Host github_two
   Hostname github.com
   User git
   IdentityFile ~/.ssh/id_rsa_two
​
   ##如果有第三個或者更多
   #gitab server 
   Host gitlab
   Hostname gitlab.com
   User git
   IdentityFile ~/.ssh/id_rsa_gitlab​

4. 測試

ssh -T git@gihub
   #如果配置正確會提示
   Hi your name one in github ! You've successfully authenticated, but GitHub does not provide shell access.
​
   ssh -T git@gihub_two
   #如果配置正確會提示
   Hi your name two in github ! You've successfully authenticated, but GitHub does not provide shell access.
​
   ssh -T git@gitlab
   #如果配置正確會提示
   Welcome to GitLab, Your GitLab nickname! 

5. Clone 項目到本地

在工作目錄下,首先先git init,然后才能使用git命令clone項目到本地  

   #之前的方式:單個賬號
   git clone git@github.com:firstAccount/xxx.git #缺省config配置時
   git clone git@github:firstAccount/xxx.git #config配置后,等價於第一條語句
   #現在要改為,git clone git@域名別稱:用戶名/項目名
   git clone git@github_two:secondAccount/xxx.git //就是使用域名地址的別名來區分
   git clone git@gitlab:gitlabAccount/xxx.git

6. 配置本地git用戶名和郵箱(非必須項)

如果首次push repo沒有配置git的賬號和郵箱,可以如下配置  

   #全局配置
   git config --global user.name "Your name"
   git config --global user.email your_email@gmail.com
​
   ##非必須項
   #局部配置,如果沒有局部配置,默認用全局配置否則優先使用局部配置
   cd ~/workspace/github_two/
   git init 
   git config  user.name "Your name"
   git config  user.email your_email@gmail.com
​

7. 小結

此篇文章是自己通過網上查詢然后自己實踐總結而得。由於本人知識有限,難免總結得很完整,如果讀者遇到什么問題,歡迎留言討論,共同學習。

8. 參考資料


免責聲明!

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



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