生成新的 SSH keys
进入用户目录下的.ssh
目录内,Windows一般为C:/Users/用户/.ssh
GitHub 的钥匙
ssh-keygen -t rsa -f C:/Users/用户/.ssh/id_rsa.github -C "邮箱"
Gitee 的钥匙
ssh-keygen -t rsa -f C:/Users/用户/.ssh/id_rsa.gitee -C "邮箱"
完成后会在.ssh/
目录下生成文件:id_rsa.github
id_rsa.github.pub
id_rsa.gitlab
id_rsa.gitlab.pub
添加识别 SSH keys 新的私钥
Windows10可能会报错,先使用管理员权限运行 PowerShell,然后执行Set-Service -Name ssh-agent -StartupType automatic
,再执行以下操作
ssh-agent bash
ssh-add C:/Users/用户/.ssh/id_rsa.github
ssh-add C:/Users/用户/.ssh/id_rsa.gitee
多账号必须配置 config 文件(重点)
在C:/Users/用户/.ssh
目录下创建config
文件,加入以下内容,这个GitHub使用 443 因为有些代理不支持 22 端口。详见 在 HTTPS 端口使用 SSH
# gitHub
Host github.com
HostName ssh.github.com
Port 443
User git
IdentityFile C:/Users/用户/.ssh/id_rsa.github
# gitee
Host gitee.com
Port 22
HostName gitee.com
User git
IdentityFile C:/Users/用户/.ssh/id_rsa.gitee
Host:当ssh的时候如果服务器地址能匹配上这里Host指定的值,则Host下面指定的HostName将被作为最终的服务器地址使用
Port:自定义的端口。默认为22,可不配置
User:自定义的用户名,默认为git,可不配置
HostName:真正连接的服务器地址
PreferredAuthentications:指定优先使用哪种方式验证,支持密码和秘钥验证方式
IdentityFile:指定本次连接使用的密钥文件
在 github 和 gitee 网站添加 ssh keys
就是对应的.pub
文件内的内容上传至对应网站
测试是否连接成功
$ ssh -T git@github.com
Warning: Permanently added the RSA host key for IP address '13.250.177.223' to the list of known hosts.
Hi dragon! You've successfully authenticated, but GitHub does not provide shell access.
$ ssh -T git@gitee.com
The authenticity of host 'gitee.com (116.211.167.14)' can't be established.
ECDSA key fingerprint is SHA256:FQGC9Kn/eye1W8icdBgrp+KkGYoFgbVr17bmjeyc.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'gitee.com,116.211.167.14' (ECDSA) to the list of known hosts.
Hi 我是x! You've successfully authenticated, but GITEE.COM does not provide shell access.
为项目配置邮箱和用户名,需要在具体的项目根目录下敲以下命令:
git config user.name 用户名
git config user.email 邮箱
为 GitHub 添加 SSH 代理
在C:/Users/用户/.ssh
目录下config
文件,加入以下内容
ProxyCommand "D:\Program Files\Git\mingw64\bin\connect.exe" -S 127.0.0.1:10808 -a none %h %p