Mac 本地项目推送至 GitHub 完整指南

第一步:检查本地 SSH 密钥

macOS 默认 SSH 密钥路径为 ~/.ssh/id_rsa(私钥)和 ~/.ssh/id_rsa.pub(公钥)。

查看是否有现有密钥

ls -la ~/.ssh/

如果没有密钥,生成一对:

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

一路回车使用默认路径即可

复制公钥内容:

cat ~/.ssh/id_rsa.pub

将输出的内容完整复制,前往 GitHub → Settings → SSH and GPG Keys → New SSH Key,粘贴并保存。

第二步:配置 SSH Config(解决 22 端口被封问题)

部分网络环境(公司网络、部分 ISP)会封禁 SSH 默认的 22 端口,导致 git push 失败。通过以下配置使用 443 端口(HTTPS 端口)绕过限制。

编辑 SSH 配置文件:

vi ~/.ssh/config

添加以下内容:

Host github.com

Hostname ssh.github.com

Port 443

User git

IdentityFile ~/.ssh/id_rsa

IdentitiesOnly yes

说明:

Hostname ssh.github.com --- GitHub 提供的 SSH-over-HTTPS 域名

Port 443 --- 走 HTTPS 端口,通常不会被防火墙拦截

IdentityFile --- 指定使用的私钥路径

IdentitiesOnly yes --- 只使用指定的密钥,避免尝试其他密钥导致失败

第三步:测试 SSH 连接

ssh -T git@github.com

成功时会输出:

Hi <你的用户名>! You've successfully authenticated, but GitHub does not provide shell access.

常见错误:

Permission denied (publickey) → 公钥未添加或 SSH config 配置错误

Connection timed out → 检查网络代理设置,或确认 SSH config 中的 Hostname 和 Port 是否正确

第四步:设置远程仓库地址

首次添加远程仓库:

git remote add origin git@github.com:<用户名>/<仓库名>.git

已有远程地址需要修改(从 HTTPS 切换为 SSH):

git remote set-url origin git@github.com:<用户名>/<仓库名>.git

验证远程地址:

git remote -v

预期输出:

origin git@github.com:<用户名>/<仓库名>.git (fetch)

origin git@github.com:<用户名>/<仓库名>.git (push)

第五步:推送代码

首次推送(设置上游分支关联)

git push --set-upstream origin main

后续推送只需:

git push

附录:HTTPS 方式(备选)

如果 SSH 配置遇到问题,可以临时使用 HTTPS 方式:

设置远程地址为 HTTPS

git remote set-url origin https://github.com/\<用户名>/<仓库名>.git

首次推送会要求输入 GitHub 用户名和 Token(不是密码)

git push --set-upstream origin main

GitHub 已废弃密码认证,需使用 Personal Access Token 代替密码。

快速操作清单

1. 生成密钥(如已有则跳过)

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

2. 复制公钥 → 添加到 GitHub Settings

cat ~/.ssh/id_rsa.pub

3. 配置 SSH(解决 22 端口问题,可选但推荐)

echo '

Host github.com

Hostname ssh.github.com

Port 443

User git

IdentityFile ~/.ssh/id_rsa

IdentitiesOnly yes

' >> ~/.ssh/config

4. 测试连接

ssh -T git@github.com

5. 设置远程地址

git remote add origin git@github.com:<用户名>/<仓库名>.git

6. 推送

git push --set-upstream origin main

相关推荐
m4Rk_1 小时前
【论文阅读】Agent 记忆机制(83):Inside Out——用可演化 PersonaTree 构建 Agent 的核心长期记忆
论文阅读·人工智能·学习·开源·github
传奇开心果编程2 小时前
【SwiftUI娓娓道来】第3课:让界面活起来——交互与动画指南
学习·macos·ui·ios·swiftui·swift
m4Rk_3 小时前
【论文阅读】Agent 记忆机制(81):EMR——用情景记忆避免 Agent 在多步推理中反复绕圈
论文阅读·人工智能·学习·开源·github
liuhl09105 小时前
【无标题】
大数据·elasticsearch·搜索引擎
u1301306 小时前
GitHub 热榜项目:周榜(2026-09-27)
人工智能·github
xbgRS6 小时前
Elasticsearch的查询
elasticsearch
朋友圈自动点赞工具7 小时前
Gmail 委托邮箱在手机上关不掉
macos·智能手机·cocoa·硬件评测
小黄人软件8 小时前
AndroidStudio老项目 macOS运行BlueTooth蓝牙串口助手(Android+Studio源码).rar
android·macos
m4Rk_9 小时前
【论文阅读】Agent 记忆机制(82):ReasoningBank——从成功与失败经验中沉淀可复用的推理记忆
论文阅读·人工智能·学习·开源·github
EatFan9 小时前
GitHub Trending 三连观察:AI 编码智能体进入“周边生态”竞争阶段
人工智能·驱动开发·github·mcp·superpowers·github trending·spec kit