手把手教你用SSH与GitHub仓库建立连接

@TOC
在使用原来的账户密码方式建立连接时,发现终端提示:

remote: Support for password authentication was removed on August 13, 2021.
remote: Please see https://docs.github.com/en/get-started/getting-started-with-git/about-remote-repositories#cloning-with-https-urls for information on currently recommended modes of authentication.

大概就是说github关闭了之前的这种不安全的方式,现在需要使用ssh来连接。

笔者系统版本为Ubuntu 20.04.6 LTS 64位,windows系统也可参照下面的步骤进行设置,唯一的不同只有路径命名有区别

1. 配置git账户的邮箱及用户名

邮箱是github的主邮箱,用户名就是github的用户名

git config --global user.name "你的用户名"
git config --global user.email "你的邮箱"

2. 生成ssh-key

这里的邮箱同样是github的主邮箱

ssh-keygen -t rsa -C "你的邮箱"

输入后提示下面的内容,直接回车选择默认路径保存(记下这个路径之后会用到)
在这里插入图片描述
设置密码
在这里插入图片描述
二次确认密码
在这里插入图片描述
二次确认完成

3.复制秘钥

在上一步显示的默认路径中找到id_rsa.pub文件,用文本编辑器打开,复制其中的内容
在这里插入图片描述

4.在github中配置秘钥

进入github -> settings -> SSH and GPG keys -> new SSH key
title中写上你的备注,在key中粘贴刚刚复制的秘钥,key type选择Authentication Key
填完之后点击Add SSH Key

5.验证是否连接成功

在终端中输入ssh -T [email protected]命令
会提示Are you sure you want to continue connecting (yes/no/[fingerprint])?
输入yes之后即可看到successfully authenticated等字样。(ubuntu系统可能会让输入系统密码来授予权限)

6.恭喜成功连接!!!

远程连接相关Git命令

  • 查看当前远程仓库
git remote -v
  • 清除当前的远程仓库
git remote rm origin
  • 设置用户名和邮箱
全局:
git config --global 用户名
git config --global 邮箱
只在当前运行目录下生效:
git config 用户名
git config 邮箱
  • 添加远程仓库
git remote add origin [email protected]:用户名/仓库名.git