为什么使用SSH的方式
如何将Git提交代码的http方式换成ssh方式
1. 生成ssh的秘钥
ssh-keygen -t rsa -b 4096 "你的git邮箱" (密码可以不设置,直接enter)
【 生成的秘钥默认在 ~/.ssh文件下(公钥和私钥)】
【 复制文件内容到剪切板:clip > 路径 (window系统) 】
2. 登录git账户,将秘钥的 公钥 复制到 “SSH keys“ (管理员)
3. 设置/更改 指定远程仓库的URL
git remote set-url origin 新地址
查看git登录的用户
git config --global user.name "your_username"
git config --global user.email "your_email@example.com"
git config --list
显示所有远程仓库: git remote -v更改远程仓库URL: git remote set-url origin 项目远程仓库路径
4. 在ssh客户端配置 私钥(以pycharm为例)
setting -> tools -> ssh configurations
host: 服务器的ip或者域名(xxx.com)port: 服务器的端口
user name: 随意
authentication type: 密钥对(Key pair)
private key file: 私钥的文件路径
最后进行Test Connection
遇到的问题:
push的时候提示类似没有什么权限,询问是否继续,yes之后会给出一个警告信息:Warning: Permanently added 'gitlab.jiufengxinxi.com,49.234.21.109' (ECDSA) to the list of known hosts.
原因:SSH客户端已经将该服务器的主机密钥(公钥)永久添加到了你本地机器上的known_hosts
文件中,这样做可以确保你的机器只会在第一次连接到服务器时提示你确认服务器的真实性,并防止未来的连接中对服务器的真实性进行重复验证。