Windows配置Github、Gitee共存的Git環境


@


在我們的日常開發中,可能要用到多個賬號,公司開發gitlab,國外開源github,國內開源gitee。這種多環境的情況下,我們就需要生成和配置多個SSH-Key。

本文基於Win10操作系統,需要安裝好Git:Windows環境下Git安裝和配置


1、清除git全局配置

如果在此之前配置過全局用戶名和郵箱:

$ git config --global user.name "test"
$ git config --global user.email test@qq.com

可以通過git config --global --list 來查看自己的全局配置。

由於是對應不同的開發場景,我們不同環境的提交可能是不同的用戶名,所以先清除這兩個配置。

$ git config --global --unset user.name "test"
$ git config --global --unset user.email test@qq.com

清楚掉全局配置以后,提交代碼還需要我們的用戶名、郵箱怎么辦?

在我們的工作目錄(項目)里配置即可:

$ git config  user.name 'test'
$ git config  user.email test@qq.com

2、SSH keys

打開 git bash。

  • 2.1、生成github用的 SSH key
$ ssh-keygen -t rsa -C 'github郵箱號' -f ~/.ssh/id_rsa_github

在這里插入圖片描述

  • 2.2、生成gitee用的 SSH key
$ ssh-keygen -t rsa -C 'gitee郵箱號' -f ~/.ssh/id_rsa_gitee

在Windos的User Home目錄(C:\Users\用戶名\.ssh)下,會生成對應的文件。

在這里插入圖片描述

  • 2.3、分別登錄giteegithub添加SSH KEY

這里以gitee為例,github和gitee類似,這里就不再演示。

  - 進入設置 --> SSH公鑰 --> 添加公鑰

在這里插入圖片描述

  - 將我們生成的密鑰 id_rsa_gitee.pub填進去

在這里插入圖片描述


3、配置config

  • 打開git bash,在.ssh目錄下,新建並編輯config文件,命令如下:
$ cd ~/.ssh
$ touch config
$ vim config
  • 輸入 i進入編輯模式,輸入以下內容
# gitee
Host gitee.com
HostName gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_gitee
# github
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_github

在這里插入圖片描述

-ESC 進入命令模式輸入:wq保存、退出。


4、用ssh命令測試

用ssh命令分別測試:

$ ssh -T git@gitee.com
$ ssh -T git@github.com

出現下面的結果說明配置成功:

$ ssh -T git@gitee.com
The authenticity of host 'gitee.com (212.64.62.183)' can't be established.
ECDSA key fingerprint is SHA256:FQGC9Kn/eye1W8icdBgrQp+KkGYoFgbVr17bmjey0Wc.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'gitee.com,212.64.62.183' (ECDSA) to the list of known hosts.
Hi fighter3! You've successfully authenticated, but GITEE.COM does not provide shell access.

在這里插入圖片描述


6、使用

  • 我們可以通過 ssh 路徑來克隆項目,或者git init之后添加遠程倉庫:

在這里插入圖片描述在這里插入圖片描述

  • 上面是一個gitee倉庫的路徑,我們可以給項目添加github的遠程倉庫
$ git remote add github git@github.com:fighter3/dairly-learn.git
  • 接下來我們可以把代碼 push 到github
$ git push github master




參考:

【1】:配置同時使用 Gitlab、Github、Gitee(碼雲) 共存的開發環境
【2】:Git配置多個SSH-Key


免責聲明!

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



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