當(dāng)使用git協(xié)議推送本地代碼到遠(yuǎn)程時,需要配置ssh連接到GitHub。
設(shè)置賬戶
打開系統(tǒng)cmd。設(shè)置Git的user name和email。
> git config --global user.name "username"
> git config --global user.email "username@domain.com"
生成ssh key
- 查看ssh密鑰
查看是否已經(jīng)有了ssh密鑰。
> C:
> cd %USERPROFILE%/.ssh
默認(rèn).ssh文件夾會在用戶文件夾中生成??梢郧巴撀窂讲榭?。如果沒有該文件夾,終端會提示系統(tǒng)找不到指定的路徑,有的話可以備份刪除。
%USERPROFILE%是環(huán)境變量,表示當(dāng)前用戶文件夾路徑。
因?yàn)?USERPROFILE%一般在C盤,從其他盤無法直接cd到該路徑,所以需要先切換到c盤。
- 生成密鑰
> ssh-keygen -t rsa -C "username@domain.com"
或者
ssh-keygen -t rsa -C "username@domain.com" -f %USERPROFILE%/.ssh/githug_blog_keys #生成ssh key的名稱為githug_blog_keys,慎用容易出現(xiàn)其它異常。
產(chǎn)生如下交互提示:
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/username/.ssh/id_rsa):
Created directory '/c/Users/username/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/username/.ssh/id_rsa.
Your public key has been saved in /c/Users/username/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:ggjGi2UqwYTJR9TP6fdpqsY+COeMdeTPL+IW0mo4eUI username@domain.com
The key's randomart image is:
+---[RSA 2048]----+
|oooo. |
|*. . . |
|o++ o . |
|o*.. . = |
|+.. . * S |
|. E + B . |
| . X * = . . |
| * B * + + |
| = =++.=. |
+----[SHA256]-----+
命令生成兩個文件,默認(rèn)名稱是id_rsa和id_rsa.pub。
復(fù)制id_rsa.pub中的內(nèi)容到遠(yuǎn)程GitHub賬號中就行了。
默認(rèn)下,cmd無法識別ssh-keygen命令??梢源蜷_git bash 執(zhí)行上述命令。
如何找到http://github.com上的ssh設(shè)置:右上角圖標(biāo)(view profile and more)=》Settings =》 SSH and GPG keys,在Key輸入框中輸入公鑰。公鑰內(nèi)容形如:
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD2cauWf8lNjbED9RvLBWoEXB3Lq5NRLWqVYSaFcTmZ1Qsak2LdR/9bkdTVMsTyqVxnB+bDMlVJlvPP6Zv5dDwEdcdytALUGCSdOXAmRtgxHZPEnKl8Hyl5wZdSNi0mwXYbjpUZ7HEw3vU8K/5whVVCbIzkqnlLAs9nXdORNkidOZRnyt+ETQzU/F1KVUb9HoMbB1Cw0zLvWTRIOHRXa2mKjNHS7W9HJiPEWQaeEXwh1CoredCxs0K7+KBfMkdgNYnDudtz2/AmH7qHnaVsdYNkg1V4XvWJ8Yn7Pkw1SpvTkaXXCiyA5wsPGgLFWSB+dgNroGwqw4X96/ZUfFHDtD/t yanyinhong@baidu.com