> For the complete documentation index, see [llms.txt](https://bugs.vongwahchau.cn/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://bugs.vongwahchau.cn/shi-yong-ssh-fang-shi-la-qu-dai-ma.md).

# 使用 SSH 方式拉取代码

> 使用 `SSH` 方式拉取代码

大家在使用 git 拉取代码时，是否曾遇到过类似 OpenSSL SSL\_read: Connection was reset, errno 10054 的报错信息，[解决方案参考](https://blog.csdn.net/m0_51269961/article/details/123709195)大概率原因出在网络身上，为避免各种修改代理，本机 host 等繁琐操作，推荐大家使用SSH方式拉取代码。那么首先需要先完成SSH基本配置，下面是详细配置过程

## 通过SSH方式拉取代码

如果你还不了解 git 中的一些基本知识，请[参考这里](https://blog.csdn.net/m0_51269961/article/details/119022739)

## 1.配置用户名和邮箱&#x20;

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

## 2.生成密钥对 首先查看设备上是否已生成过秘钥对

```sh
cd ~/.ssh //进入指定路径文件夹 
ls //查看文件夹下内容 
```

看一下有没有 <mark style="color:red;">id\_rsa</mark> 和 <mark style="color:red;">id\_rsa.pub</mark> 等文件，<mark style="color:red;">.pub</mark> 文件是[<mark style="color:orange;">公钥</mark>](https://so.csdn.net/so/search?q=%E5%85%AC%E9%92%A5\&spm=1001.2101.3001.7020)，另一个文件是密钥

若没有这些文件，或没有 <mark style="color:red;">.ssh</mark> 目录，则使用 <mark style="color:red;">ssh-keygen</mark> 命令来创建

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

&#x20;如有提示信息，点击 enter 即可，<mark style="background-color:yellow;">不需要设置密码！</mark> 难道你想在每次提交代码前输入一遍密码吗？

成功后会提示

```bash
Your public key has been saved in /home/you/.ssh/id_rsa.pub. The key fingerprint is:
```

## 3.进入 .ssh 文件夹，查找公钥&#x20;

进入指定路径 <mark style="color:red;">.ssh</mark> 文件夹中，用记事本打开 <mark style="color:red;">id\_rsa.pub</mark>，全选复制内容

也可通过指令查看 *win可以用* <mark style="color:red;">`type`</mark>*命令查看*

```bash
cat ~/.ssh/id_rsa.pub
```

例如：

```bash
ssh-rsa your secret email@email.com
```

## 4.进入代码托管平台，上传密钥&#x20;

* 下面以 GitHub 为例，演示相关操作过程
  1. 登陆 github 帐户，点击你的头像，然后 <mark style="color:red;background-color:red;">Settings -> SSH and GPG keys -> New SSH key</mark>
  2. 然后你复制上面的公钥内容，粘贴进 <mark style="color:red;">key</mark> 文本域内。 <mark style="color:red;">title</mark> 域，自己随便起个名字
  3. 点击 <mark style="color:red;">add key</mark>

完成以后，验证下这个key是不是正常工作：

```bash
ssh -T git@github.com 
Attempts to ssh to github
```

如果，看到如下信息提示

```bash
Hi xxx! You've successfully authenticated, but GitHub does not # provide shell access. 
```

&#x20;恭喜，配置成功！

## 5.找到项目仓库 SSH 地址&#x20;

使用命令 <mark style="color:red;">git remote -v</mark> 查看你当前的 <mark style="color:red;">remote url</mark>

```git
git remote -v 
origin https://github.com/username/project.git (fetch) 
origin https://github.com/username/project.git (push) 
```

如果是以上的结果那么说明此项目是使用 <mark style="color:red;">https</mark> 协议进行访问的；如果地址是 <mark style="color:red;">git</mark> 开头则表示是 <mark style="color:red;">git</mark> 协议

登陆你的 <mark style="color:red;">github</mark> 账户，查看项目仓库 <mark style="color:red;">ssh</mark> 协议的 <mark style="color:red;">URL</mark>&#x20;

<figure><img src="/files/JO7eHs6PwlcyE0CxXGAx" alt=""><figcaption></figcaption></figure>
