Git密钥并连接至Github - 2024最新教程

3.配置 Git 密钥并连接至 Github

常用 Git 命令

1
2
3
git config -l  //查看所有配置
git config --system --list //查看系统配置
git config --global --list //查看用户(全局)配置

1.1. 配置用户名和邮箱

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

通过 git config -l 检查是否配置成功。
alt text

1.2. 配置公钥连接 Github

执行以下命令生成 ssh 公钥,此公钥用于你的计算机连接 Github

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

提示 Enter file in which to save the key 直接一路回车即可,新手小白不推荐设置密钥
alt text

之后打开 C 盘下用户文件夹下的.ssh 的文件夹,会看到以下文件
id_rsa私钥
id_rsa.pub公钥
https://img.090227.xyz/file/40bd636f749347666f9f7.jpg
用记事本打开上述图片中的公钥id_rsa.pub,复制里面的内容,然后开始在 github 中配置 ssh 密钥。
https://img.090227.xyz/file/0086246f3ccc703b9035e.jpg
将 SSH KEY 配置到 GitHub
进入 github,点击右上角头像 选择 settings,进入设置页后选择 SSH and GPG keys,名字随便起,公钥填到 Key 那一栏。
alt text
alt text
alt text
alt text
测试连接,输入以下命令

1
ssh -T [email protected]

第一次连接会提示Are you sure you want to continue connecting (yes/no/[fingerprint])?,输入 yes 即可
alt text
出现连接到账户的信息,说明已经大功告成,至此完成了环境准备工作。
alt text

1.3. 创建 GitHub.io 仓库

点击右上角的+按钮,选择New repository,创建一个<用户名>.github.io的仓库。
仓库名字的格式必须为:<用户名>.github.io (注意:前缀必须为用户名,此为预览博客需要,后期可修改仓库名)
可见性必须选择 Public 方便第一次部署检查问题,点击Creat repository进行创建即可
alt text