首页 > 系统相关 >从零开始:配置 SSH 并将 Git 项目推送到 GitHub(windows)

从零开始:配置 SSH 并将 Git 项目推送到 GitHub(windows)

时间:2024-05-31 18:34:07浏览次数:16  
标签:GitHub windows rsa Git SSH git 推送 com

项目推送到github总是遇到如下问题:

fatal: unable to access 'https://github.com/lw1725908379/power_supply.git/': Failed to connect to github.com port 443 after 21060 ms: Timed out

当你使用 HTTPS 协议推送代码到 GitHub 时,Git 尝试在端口 443 上与 GitHub 的服务器建立连接。然而,有时网络环境的限制或防火墙设置可能会阻止这种连接,导致连接超时错误。

通过配置 SSH,你实际上是在使用 SSH 协议与 GitHub 通信,而不是 HTTPS。SSH 协议默认使用端口 22,这通常不受网络限制。因此,当你将远程仓库的 URL 修改为 SSH 格式后,Git 将尝试通过 SSH 连接到 GitHub,从而避免了可能存在的网络连接问题。

配置 SSH 并推送 Git 项目到 GitHub

1. 安装和配置 SSH

生成 SSH 密钥

首先,使用以下命令生成一个新的 SSH 密钥:

ssh-keygen -t rsa -b 4096 -C "your [email protected]"

解释:

  • -t rsa:指定密钥类型为 RSA。
  • -b 4096:指定密钥长度为 4096 位。
  • -C "your [email protected]":使用你的邮箱作为注释。

按照提示保存密钥和设置密码

(base) E:\WORKS\py\projects\power_supply>ssh-keygen -t rsa -b 4096 -C "your [email protected]"
Generating public/private rsa key pair.
Enter file in which to save the key (C:\Users\17259/.ssh/id_rsa):
C:\Users\17259/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in C:\Users\17259/.ssh/id_rsa
Your public key has been saved in C:\Users\17259/.ssh/id_rsa.pub
The key fingerprint is:

The key's randomart image is:
+---[RSA 4096]----+
|             . +*|
|              =o+|
|           o =.+o|
|       .  . = *Eo|
|      . S o..=.+ |
|       . . * OOo |
|          + B=== |
|         . oo B +|
|           ..=ooo|
+----[SHA256]-----+

添加 SSH 公钥到 GitHub

显示 SSH 公钥并复制:

cat ~/.ssh/id_rsa.pub

或者直接找当 'id_rsa.pub'文件复制,默认存储在C:\Users\用户名/.ssh/id_rsa.pub

将显示的公钥复制到 GitHub 账户的 SSH 设置中:

  1. 登录 GitHub。
  2. 进入 Settings > SSH and GPG keys > New SSH key
  3. 粘贴你的公钥,并保存。

2. 配置 Git 使用 SSH 远程仓库

更改远程仓库 URL 为 SSH

在你的项目目录中,打开终端(Terminal)或 Git Bash,执行以下命令:

git remote set-url origin [email protected]:lw1725908379/power_supply.git

确认远程仓库 URL 已更改

git remote -v

输出应类似于:

origin  [email protected]:lw1725908379/power_supply.git (fetch)
origin  [email protected]:lw1725908379/power_supply.git (push)

3. 推送代码到 GitHub

添加并提交更改

git add .
git commit -m "Test SSH setup"

推送更改

git push origin main

处理 SSH 主机验证提示

在首次通过 SSH 连接到 GitHub 时,你会看到以下提示:

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 并按回车键。

处理推送问题

如果遇到 error: src refspec main does not match any 错误,说明本地没有 main 分支。可以创建并切换到 main 分支:

git checkout -b main
git push --set-upstream origin main

通过以上步骤,你应该能够成功配置 SSH 密钥,并通过 SSH 将代码推送到 GitHub。如果遇到其他问题,可以检查 SSH 密钥配置和网络连接是否正常。

标签:GitHub,windows,rsa,Git,SSH,git,推送,com
From: https://www.cnblogs.com/alohablogs/p/18225087

相关文章

  • 向GitHub远程仓库同步文件使用经验【2】
    新手流畅一顿操作由于没搞懂CSDN的更新文档策略,只能把新写的内容作为新文章发布了。前一篇文章在这将本地仓库与远程仓库同步当本地仓库没修改,但远程仓库修改了,这时可以吧远程仓库同步到本地仓库===方法一===gitfetch //将本地仓库中的远程分支更新成了远程仓库相应......
  • git-05标签操作
    1.创建标签首先,确保你在要打标签的分支上。例如,如果你想给main分支打标签,可以先切换到main分支:gitcheckoutmain然后,使用以下命令创建标签:例如,创建一个名为v1.0的标签:gittag<tagname>gittagv1.02.创建带有注释的标签例如,创建一个名为v1.0的带有注释的......
  • GitLab clone 地址不对的解决办法
    1丶问题描述2丶解决方案解决方案:找到挂载到宿主机配置文件:gitlab.rbvigitlab.rb改成自己的ip 重启容器dockerrestartgitlab如果发现容器一直重启,可采用粗暴的方法,直接干掉当前容器,重新运行一个#停掉容器dockerstop容器id #删除容器 dockerrm容器id  重新运行......
  • Windows server 2022从基础到进阶的 DNS 管理内容,适合初学者快速入门并建立起对 DNS
    关于WindowsServer2022DNS管理器初级应用的大纲:1. 介绍DNS服务1.1什么是DNS?1.2DNS的作用和重要性1.3DNS解析过程概述2. 部署DNS服务器2.1安装DNS服务器角色2.2配置DNS服务器基本设置2.3DNS服务器的启动和停止3. 管理DNS区域3.1什么......
  • Git 远程代码执行漏洞(CVE-2024-32002)复现
    Git远程代码执行漏洞(CVE-2024-32002)漏洞标题:Git远程代码执行漏洞(CVE-2024-32002)影响版本:version2.45.*<2.45.1version2.44.*<2.44.1version2.43.*<2.43.4version2.42.*<2.42.2version2.41.*<2.41.1version2.40.*<2.40.2version2.39.*<2.39.4修复......
  • Repo, git相关经验
    RepoRepocomplementsGitbysimplifyingworkacrossmultiplerepositories.Fromhttps://source.android.com/docs/setup/create/repo基本语法repocontentcommandHelpRepohelpCommandHelpRepo<command>--helpinitRepoinit-uurl[option......
  • msf攻击windows实例
    环境:攻击机kali(192.168.129.139),目标机windows10(192.168.129.132)方法一:通过web站点,使用无文件的方式攻击利用执行(命令执行漏洞)方法二:通过web站点,上传webshel,返回给msf(msfvenom)方法三:攻击其他端口服务,拿到meterpreter(445)一、Nmap端口收集nmap-sV-T4 192.168.129.132(......
  • C# 检测并重启windows服务,IIS应用池
      usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Diagnostics;usingSystem.Linq;usingSystem.ServiceProcess;usingSystem.Text;usinglog4net;usingSystem.Timers;usingSystem.Configurati......
  • 两台windowserver服务器配置Redis哨兵集群
    十年河东,十年河西,莫欺少年穷学无止境,精益求精redis下载地址:https://github.com/tporadowski/redis/releases 这里选择压缩版,不选择安装版1、集群环境 主机master:局域网IP  172.27.40.27从机slave:局域网IP  172.27.40.29 2、修改主从相关配置-两台服务器均需修改......
  • idea将代码上传到gitee上
    先将代码上传到git本地仓库中操作流程:如图注意项目介绍不能为空,随便写写就行这样就上传到了本地仓库之后在上传到gitee远程仓库中    gitee官网地址:Gitee-基于Git的代码托管和研发协作平台面向企业提供一站式研发管理解决方案,包括代码管理、项目管理、文......