github与git bash绑定问题

当输入$ ssh -T git@github.com 时,

返回ssh: connect to host github.com port 22: Connection refused,

解决方法:
  • 使用 HTTPS 代替 SSH 如果你无法通过 SSH 连接,你可以改用 HTTPS 克隆仓库,而不是 SSH。 使用 git clone https://github.com/username/repository.git 来替代使用 SSH 链接的方式。

  • 使用 GitHub 的 SSH 端口 443 GitHub 支持通过端口 443(通常用于 HTTPS 流量)连接,绕过某些防火墙的限制。你可以配置 Git 使用端口 443 来连接 GitHub:

    运行以下命令将 Git 配置为使用端口 443:

    复制代码
    git config --global url."ssh://git@ssh.github.com:443".insteadOf git@github.com:
    
    然后再尝试运行 ssh -T git@github.com 看是否能成功连接。

忘记了,总之:

已成功配置了 Git 以使用端口 443,并且你的 SSH 密钥也已添加到 SSH Agent 中,但连接仍然无法成功。问题可能出在端口 22 的连接被阻止时,配置未完全生效,或者你还在尝试连接到 GitHub 的默认端口(22)。你可以尝试以下步骤来进一步解决问题:

1. 确保 Git 配置已生效

在尝试连接之前,首先确认你的配置已经正确生效。运行以下命令检查配置:

复制代码
git config --global --get-regexp url

应该会输出如下内容,确认你已将 GitHub SSH 连接指向端口 443:

复制代码
url.ssh://git@ssh.github.com:443.insteadOf git@github.com:

2. 尝试连接到 GitHub 的 SSH 服务端口 443

由于你的 Git 配置是使用端口 443 代替端口 22 进行连接,请确保你使用正确的命令来连接。你可以尝试如下:

复制代码
ssh -T git@ssh.github.com

然后:Hi Westredtomato! You've successfully authenticated, but GitHub does not provide shell access.

成功了!通过端口 443 成功地设置了 GitHub 的 SSH 连接,这通常是用来绕过防火墙或某些网络限制。你已经实现了 GitHub 账户与本地 Git Bash 的 SSH 连接绑定。

可以运行以下命令验证你已经成功与 GitHub 账户建立了 SSH 连接:

复制代码
ssh -T git@ssh.github.com

如果你看到类似下面的输出,说明你的连接已成功建立:

复制代码
Hi <your-username>! You've successfully authenticated, but GitHub does not provide shell access.

ssh -T git@ssh.github.com 可以成功连接,SSH配置和密钥是正确的,但默认的 git@github.com 连接失败。

解决方案

1. 修改 ~/.ssh/config 文件

确保你的 ~/.ssh/config 文件内容如下:

config

复制代码
Host github.com
  Hostname ssh.github.com  # 强制使用443端口
  User git
  Port 443
  IdentityFile ~/.ssh/id_rsa

保存后,测试连接:

复制代码
ssh -T git@github.com
  • 如果成功,会显示 Hi 你的用户名! You've successfully authenticated...

2. 直接使用 ssh.github.com

如果你不想修改配置文件,可以直接使用 ssh.github.com 进行Git操作:

复制代码
git clone git@ssh.github.com:name/repository.git
  • 这种方式会绕过22端口,直接使用443端口。

(这样子可行,注意不要使用git@github.com:name/repositories.git了,在前面加ssh.,改用git@ssh.github.com:name/repositories.git)

注意:Git Bash 不识别 Windows 路径中的反斜杠 \。在 Git Bash 中,路径应该使用正斜杠 /,而不是反斜杠 \

相关推荐
闲云一鹤3 小时前
Git LFS 扫盲教程 - 你不会还在用 Git 管理大文件吧?
前端·git·前端工程化
砖厂小工6 小时前
用 GLM + OpenClaw 打造你的 AI PR Review Agent — 让龙虾帮你审代码
android·github
程序员鱼皮6 小时前
又一个新项目完结,我要出海了!
ai·github·开源项目
徐小夕7 小时前
pxcharts-vue:一款专为 Vue3 打造的开源多维表格解决方案
前端·vue.js·github
Moment7 小时前
想要长期陪伴你的助理?先从部署一个 OpenClaw 开始 😍😍😍
前端·后端·github
我叫黑大帅7 小时前
前端如何利用 GitHub Actions 自动构建并发布到 GitHub Pages?
前端·面试·github
HelloGitHub9 小时前
这个年轻的开源项目,想让每个人都能拥有自己的专业级 AI 智能体
开源·github·agent
刘发财15 小时前
弃用html2pdf.js,这个html转pdf方案能力是它的几十倍
前端·javascript·github
sunny8651 天前
Claude Code 跨会话上下文恢复:从 8 次纠正到 0 次的工程实践
人工智能·开源·github