解决 GitHub Connection refused 无法访问

背景

当我们在向 GitHub 推送代码的时候,经常会遇到推不上去的情况,往往提示的是:

bash 复制代码
fatal: unable to access 'https://github.com/xxx.git/': Failed to connect to github.com port 443: Connection refused

或者是

bash 复制代码
Failed to connect to github.com port 443: Timed out

以上两种情况,大概率是 GitHub IP 无法访问导致的,通过设置代理可以解决此问题。

代理配置

有了 VPN 代理后,设置 git 命令的代理配置,有三种代理方式。

  • 全局代理
  • 本地代理,按项目设置代理
  • 按源代理,按请求地址设置代理

全局代理

通过 git config --global 可以设置全局代理,命令及示例如下:

bash 复制代码
# 设置代理,http 和 https 都加上代理,代理到 http://127.0.0.1:1087 这个 vpn 本地 IP
git config --global http.proxy http://127.0.0.1:1087 
git config --global https.proxy http://127.0.0.1:1087

# 取消代理
git config --global --unset http.proxy
git config --global --unset https.proxy

# 查看代理
git config --global --get http.proxy
git config --global --get https.proxy

# 查看全局所有配置
git config --global --list

本地代理

全局代理会将所有的 git 请求都使用这个代理,对于国内的或者公司内部的仓库,实际上是不需要的,加了反而拖慢速度。

故可以指定是 GitHub 的项目才走代理,其他的项目不走代理。

通过 git config --local 设置本项目代理,命令及示例如下:

bash 复制代码
# 设置代理
git config --local http.proxy http://127.0.0.1:1087 
git config --local https.proxy http://127.0.0.1:1087

# 取消代理
git config --local --unset http.proxy
git config --local --unset https.proxy

按源代理

本地代理的不方便之处是,每个需要代理的 GitHub 项目都需要手工配置一次,实在麻烦。

所以可以通过直接修改 git 全局配置文件的方式,指定哪些请求源走代理,不指定的就不走代理。

修改全局 .gitconfig 配置文件(一般在 ~/.gitconfig),增加如下代理配置即可。

bash 复制代码
# 配置 http 代理
[http "https://github.com"]
  proxy = http://127.0.0.1:1087  

# 配置 https 代理
[https "https://github.com"]  
  proxy = http://127.0.0.1:1087

查找代理 IP

不同的 VPN 代理软件地址可能不一样,v2ray-core 可以在如下菜单找到

Git Tower 代理问题

Git Tower 可视化工具需要设置 httpsslverify 代理才能识别。

www.git-tower.com/help/guides...

GitHub 登录问题

参考:davyjones2010.github.io/2022-07-23-...

通过命令行登录会遇到以下两种方式:

  • 如果 clone 仓库用的是 SSH,那么使用 SSH 的方式登录即可。
  • 如果 clone 仓库用的是 https,那么需要输入用户名与密码。
    • 不过从 2021 年 8 月 13 日开始,GitHub 已经 禁止了用户名 + 密码方式 登录。
    • 不过可以使用 用户名 + Personal Access Token 的方式登录,与 Tower 等 GUI 工具一样。
相关推荐
张居斜2 小时前
GitHub Actions + 阿里云 OSS:OIDC 免密同步构建产物
github·oss·llm-wiki
用户3228360084475 小时前
python-rapidjson:用 C++ 速度处理 JSON 的 Python 库
github
逛逛GitHub5 小时前
4 个比较实用的 GitHub 开源项目,浅浅的收藏一波。
github
Hommy886 小时前
【剪映小助手】添加贴纸接口(Add Sticker)
后端·github·剪映小助手·视频剪辑自动化·剪映api
noravinsc9 小时前
关于Git Flow
git
2601_961845159 小时前
粉笔行测5000题电子版|pdf|解析
pdf·新媒体运营·github·个人开发·内容运营·规格说明书·极限编程
蜜獾云9 小时前
在Git中配置用户名和密码
git
scx_link11 小时前
通过git bash在本地创建分支,并推送到远程仓库中
开发语言·git·bash
用户7735300845112 小时前
gorillamux:Go语言路由库的实用选择
github
BBWEYY终身尊贵会员13 小时前
2026年6月四款建站工具怎么选?BBWEYY、比文云、GitHub Copilot、Dreamweaver 简明对比
github·copilot·dreamweaver