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

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

适用场景:已在本地初始化 Git 仓库,需要推送到 GitHub 远程仓库。

前置条件

  • 已安装 Git(macOS 自带或 brew install git
  • 已注册 GitHub 账号
  • 已在 GitHub 创建空的远程仓库(不要勾选 "Add a README file",否则会产生冲突)

第一步:检查本地 SSH 密钥

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

bash 复制代码
# 查看是否有现有密钥
ls -la ~/.ssh/

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

bash 复制代码
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
# 一路回车使用默认路径即可

复制公钥内容:

bash 复制代码
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 配置文件:

bash 复制代码
vi ~/.ssh/config

添加以下内容:

bash 复制代码
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 连接

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

成功时会输出:

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

常见错误:

  • Permission denied (publickey) → 公钥未添加或 SSH config 配置错误
  • Connection timed out → 检查网络代理设置,或确认 SSH config 中的 Hostname 和 Port 是否正确

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

首次添加远程仓库:

bash 复制代码
git remote add origin git@github.com:<用户名>/<仓库名>.git

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

bash 复制代码
git remote set-url origin git@github.com:<用户名>/<仓库名>.git

验证远程地址:

bash 复制代码
git remote -v
# 预期输出:
# origin  git@github.com:<用户名>/<仓库名>.git (fetch)
# origin  git@github.com:<用户名>/<仓库名>.git (push)

第五步:推送代码

bash 复制代码
# 首次推送(设置上游分支关联)
git push --set-upstream origin main

# 后续推送只需:
git push

附录:HTTPS 方式(备选)

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

bash 复制代码
# 设置远程地址为 HTTPS
git remote set-url origin https://github.com/<用户名>/<仓库名>.git

# 首次推送会要求输入 GitHub 用户名和 Token(不是密码)
git push --set-upstream origin main

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


快速操作清单

bash 复制代码
# 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
相关推荐
dong_junshuai3 小时前
每天一个开源项目#26 Caveman:8.2万星的 AI 口语压缩技能,输出减 75%
github
wangruofeng4 小时前
68K Star,一个爬虫框架凭什么统一了 HTTP、浏览器和 AI Agent
爬虫·github·ai编程
AC赳赳老秦7 小时前
采购专员自动化:OpenClaw 自动比价、生成询价单、跟踪供应商报价进度实战指南
开发语言·数据库·人工智能·python·自动化·github·openclaw
white_ant7 小时前
GitLab 迁移
github
xurime18 小时前
Excelize 开源十周年,发布 2.11.0 版本
golang·开源·github·excel·导出·导入·excelize·基础库
逛逛GitHub1 天前
自测会不会被 Claude Code 标记为中国用户,有人做了个网页。
github
逛逛GitHub1 天前
终于有个非 AI 相关的项目登上 GitHub 热榜,高低得推荐一下。
github
Lion091 天前
【04】50 行代码实现最小 Agent:不依赖任何框架
人工智能·github
狂炫冰美式1 天前
凌晨睡不着,我给台风巴威写了个追踪网站
前端·后端·github