用SSH来连接Github

如果用SSH方法来git clone github repo的话,就得通过以下步骤来做。
博主是以Macbook为例的,如果你是用windows或者linux的,就自己查查怎么安装吧!

  1. 检查本地电脑里有没有SSH keys
    [1] 打开Terminal
    [2] 输入 ls -al ~/.ssh这个命令来检查是否已经有SSH keys
    一般默认情况下的 SSH key 的格式如下:
    (1)id_rsa.pub
    (2)id_ecdsa.pub
    (3)id_ed25519.pub
    如果发现都没 SSH key, 那么就得创建一个新的 SSH key
  2. 创建一个新的SSH key
    [1] 打开 Terminal
    [2] 输入这个命令
$ ssh-keygen -t ed25519 -C "your_email@example.com"

提示:
如果你的苹果电脑系统用的legacy system, 这是不支持 Ed25519的,那么就得使用下面的命令:

$ ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

输入完上面的任何一个命令之后,就会看到这个结果:

> Generating public/private rsa key pair.

紧接着看到另外一个结果:

> Enter a file in which to save the key (/Users/you/.ssh/id_rsa): [Press enter]

这是默认的存key的地址,直接按’enter’就行了

然后就会看到下面的结果:

> Enter passphrase (empty for no passphrase): [Type a passphrase]
> Enter same passphrase again: [Type passphrase again]

输入完之后,就看到你的identification和public key存在的地方,你的key fingerprint也可以知道,和你的key’s randomart image也可以看到

  1. 将你的SSH key 加入到ssh-agent
    [1] 在后台开始ssh-agent,命令如下:
$ eval "$(ssh-agent -s)"
> Agent pid 59566

提示:这个Agent pid有可能会跟你的不一样。

[2] 如果你的macOS Sierra 10.12.2 or later,you will need to modify your ~/.ssh/config file to automatically load keys into the ssh-agent and store passphrases in your keychain.
(a) First, check to see if your ~/.ssh/config file exists in the default location.

$ open ~/.ssh/config
> The file /Users/you/.ssh/config does not exist.

(b) 如果文件不存在,就得创建一个新的文件

$ vim ~/.ssh/config

( C ) Open your ~/.ssh/config file, then modify the file to contain the following lines. If your SSH key file has a different name or path than the example code, modify the filename or path to match your current setup. 粘贴下面的代码

Host *
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_rsa

[3] Add your SSH private key to the ssh-agent and store your passphrase in the keychain. If you created your key with a different name, or if you are adding an existing key that has a different name, replace id_rsa in the command with the name of your private key file. 输入下面的命令

$ ssh-add -K ~/.ssh/id_rsa
  1. 添加新的SSH key
    [1] 在添加ssh key之前,先检查,是否已经在本地电脑里有ssh key了
ls -al ~/.ssh

[2] 已经有了ssh key了,就Copy the SSH public key to your clipboard.
If your SSH public key file has a different name than the example code, modify the filename to match your current setup. When copying your key, don’t add any newlines or whitespace.

$ pbcopy < ~/.ssh/id_rsa.pub
# Copies the contents of the id_rsa.pub file to your clipboard

[3] 去到 Github -> setting -> SSH and GPG keys -> New SSH key
请添加图片描述
[4] 将刚刚已经复制好的ssh key,粘贴到 key区域里,不能添加或者减少任何的字符,包括空格
请添加图片描述
[5] 点击 Add SSH key按钮

  1. 测试你的SSH Connection
    进入到你任何一个repo,尝试一下git push,有可能你会碰到无法git push, 那么你就得用下面的命令来做git push
git push -f origin main

这样以后就可以直接

git push origin main

希望这篇博客,能给你带来帮助。
既然都看到这了,就点赞关注留言一下呗,你的支持是对博主最大的鼓励 ^ ^

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
SSH连接GitHub的步骤如下: 1. 首先,确认你还没有设置SSH。打开终端,输入cd ~./ssh(默认目录)或者其它你设置过的ssh目录位置并回车。如果终端返回No such file or directory,那说明你应该新设置一个SSH。 2. 生成SSH密钥。在终端输入ssh-keygen -t rsa并回车。按两次回车,第一次选择SSH密钥的存储位置,默认位置是/home/\[用户名\]/.ssh/id_rsa;第二次回车时选择passphrase,可以选择留空或者设置一个密码。 3. 确认生成SSH密钥。在终端内重新输入步骤1的指令cd ~/.ssh和ls,确认SSH密钥文件已被生成。 4. 复制SSH密钥。在终端输入cat id_rsa.pub获取SSH公共密钥内容,复制ssh-rsa ... @mail.ericfrenzy.fun。 5. 在GitHub上添加SSH密钥。在设置页面找到SSH and GPG keys,点击New SSH key。在Title处填写一个你能认出的名字,将刚才复制的SSH公共密钥粘贴到下方的文字框内,点击Add SSH key。 6. 测试连接。在终端输入ssh -T git@github.com并回车。输入yes确认连接。如果出现Hi xxx! You've successfully authenticated, but GitHub does not provide shell access.,说明SSH密钥配置成功。 请注意,在使用git clone等命令时,要使用SSH的URL而不是HTTPS的。 以上是连接GitHubSSH步骤。\[2\] #### 引用[.reference_title] - *1* [使用 ssh 连接 Github 代码库超详细教程](https://blog.csdn.net/snsHL9db69ccu1aIKl9r/article/details/101803787)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [在本地配置SSH连接GitHub以及其它服务器(详细图文教程)](https://blog.csdn.net/EricFrenzy/article/details/126904024)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值