Git安装配置与远程仓库使用
1. Git 下载与安装
① 进入Git 官网 https://git-scm.com/
② 选择合适系统版本下载,本文以windows为例进行下载
当前最新版本为 2.50.1 ,浏览器默认下载很慢,用迅雷比较快
③ 安装Git
我安装在D盘
等待完成即可
2. Git 配置
① 配置用户名和电子邮箱(用来记录代码提交者)
在任意文件夹空白处右键,选择 Open Git Bash here,
配置如下信息:
git config --global user.name "your name"git config --global user.email "your_email@163.com"
使用如下命令查看配置是否生效:
git config --global --list
② 生成 SSH Key
在 Git 中添加 SSH Key 的主要作用是安全、方便地进行身份验证,让你无需每次都输入用户名和密码就能与远程 Git 仓库(如 GitHub, GitLab, Gitee, 自建 Git 服务器等)进行安全的通信。
-
进入git Bash后执行如下命令:
ssh-keygen -t rsa -C "your_email@163.com"
直接一直回车就好 -
找到公钥位置
上述步骤执行完后会在提示的文件夹下生成公钥(id_rsa.pub)和私钥(id_rsa),这里涉及到非对称加密的知识,简而言之 明文+公钥 经过加密算法变成密文,密文+私钥经过解密算法变成明文,不做过多解释
-
复制公钥 id_rsa.pub 内容
③ 配置 SSH Key
1)进入github设置页面
2)选择 SSH and GPG keys
3)新增 SSH Key
把公钥 id_rsa.pub 内容复制进来即可
成功后可看到
4)验证
在 git bash 命令框输入如下命令:
ssh -T git@github.com
如果是第一次执行该命令会看到如下信息:
The authenticity of host 'github.com (20.205.243.166)' can't be established.
ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])?
意思是无法确认主机真实性,问我们是不是要继续连接,我们输入 yes 回车即可,之后可以看到如下内容:
Warning: Permanently added 'github.com' (ED25519) to the list of known hosts.
Hi 你的github名称! You've successfully authenticated, but GitHub does not provide shell access.
这样就表示本地与 github(远程仓库)成功建立了连接