工做緣由須要常用 Gitee(碼雲)和 Github

既然要同時配置 Gitee 和 Github ,因此如下這些步驟天然是要執行兩遍的。通常來講,在 Gitee 上和在 Github 上使用的帳號郵箱應該是不同的,因此配置的時候要特別注意一下。html

建立 ssh key

# 進入用戶目錄下的 .ssh 文件夾下,路徑會因你使用的操做系統不一樣而略有差別
# 沒有這個文件夾也無所謂,直接運行下一句命令也能夠
cd ~/.ssh

# 生成 key,將郵件地址替換爲你 Gitee 或者 Github 使用的郵件地址
ssh-keygen -t rsa -C "xxx@xxx.com"

window下通常在c:/用戶/當前用戶名/.ssh  找下很容易能看到git

接下來應該會看到下面的提示:github

Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/your_user_name/.ssh/id_rsa): id_rsa_gitee

這一步若是默認回車,會生成名爲 id_rsa 的文件,你能夠輸入不一樣的名字來便於識別文件,好比生成 Gitee 的 ssh key 能夠設置爲 id_rsa_gitee,設置 Github 的 ssh key 能夠設置爲 id_rsa_github ,我這裏設置爲 id_rsa_giteeshell

接下來的會看到:windows

Enter passphrase (empty for no passphrase):

直接回車,而後會看到:bash

Enter same passphrase again:

繼續回車就好了。生成完畢:dom

Your identification has been saved in id_rsa_gitee.
Your public key has been saved in id_rsa_gitee.pub.
The key fingerprint is:
SHA256:F0K/ojCbFzgMPru11m4g/9uV03oHK+U0rKBLwOOye2c xxx@xxx.com
The key's randomart image is:
+---[RSA 2048]----+
|        .        |
|       . .       |
|  .     . o      |
| . + .   . o     |
|  o X . S o.     |
|  .+.O o.o o*    |
|  oo=o+. .+=.+   |
|   =++E. .oo+ .  |
|  ++.*=o. .o .   |
+----[SHA256]-----+

在 Gitee 和 Github 添加 public key

找到用戶目錄下的 .ssh 文件夾,查看並複製建立好的 id_rsa_gitee.pub  id_rsa_github.pub 的內容。ssh

cd ~/.ssh
# 查看 id_rsa_gitee.pub 文件內容
cat id_rsa_gitee.pub

會顯示這樣一串東西,複製它:ide

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDZbvgUEj3XAXH4HkW27ibdXgV6VHdrA9/WdSDHtiiC55mjPvxj3OtPxIbpeJmhWyHiJWR6
uUuK+gkb//O51uWCPhHqxKR7+45tZ9jHqXW+hEKPp+odQgc+3hiHAjTkn3JGeIJlQp2UdJCDHBrp+kcgVeg91+y7cU3ufaUQ/hpD
rCgn6uvwjwJgnDhV9DYi+gFUFe7LUwa1o4nfwg43ycuOOuT7c6VO2dj/0pLRUVTPQYu/C3kaaPVedir7mKIu/dM6Ec44bhYTp1Dq
qp8BO42Cfo+n+dempqYTe2wcPvuDjSj884IATc/KvBfc86Yd2Uj7NI7li90Y3i6adoxUIWQh xxx@xxx.com

打開 Gitee 和 Github 的網站找到設置,再找到 SSH Keys,添加複製的 public key 。gitlab

Gitee 新增 ssh key

Github 新增 ssh key

建立配置文件

在 .ssh 文件夾中建立 config 文件,添加如下內容以區分兩個 ssh key:

# 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

測試鏈接是否正常

在命令行輸入:

ssh -T git@github.com

若返回以下內容,則 Github 鏈接正常:

Hi yourname! You've successfully authenticated, but GitHub does not provide shell access.

繼續在命令行輸入:

ssh -T git@gitee.com

若返回以下內容,則 Gitee 鏈接正常。

Welcome to Gitee.com, yourname!

(完)

 

 

https://www.cnblogs.com/Allen-rg/p/10382342.html    這文章裏有建立ssh config文件的方法

一、建立config文件,將文件建立在【.ssh】目錄下
①在windows下新建一個txt文本,而後將名字改爲config(包括.txt後綴)
②在git bash下,直接touch config便可建立一個config文件

二、編輯config文件,修改以下內容:

# gitlab Host gitool.glanway.com HostName gitool.glanway.com PreferredAuthentications publickey IdentityFile ~/.ssh/id_rsa_gitlab User yangjie # github Host github.com HostName github.com PreferredAuthentications publickey IdentityFile ~/.ssh/id_rsa_github User yangjie 

配置文件說明:
每一個帳號單獨配置一個Host,每一個Host要取一個別名,每一個Host主要配置HostNameIdentityFile兩個屬性便可
Host的名字能夠取爲本身喜歡的名字,不過這個影響git相關命令
例如:
Host mygithub 這樣定義的話,命令以下,即git@後面緊跟的名字改成mygithub
git clone git@mygithub:PopFisher/AndroidRotateAnim.git
就至關於你配置的HostName真正的域名,映射成了Host後面的配置的名字

相關文章
相關標籤/搜索