You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法向GitHub仓库推送代码(403/SSH公钥权限问题)

排查GitHub推送权限问题的小众方向

问题场景

尝试向已获得访问权限的GitHub仓库推送代码,执行常规操作:

git add .
git commit -m "message"
git push origin master

系统要求输入用户名和密码,尝试个人访问令牌及GitHub登录密码后,均收到如下错误:

remote: Permission to uName/repo.git denied to uName.
fatal: unable to access 'https://github.com/uName/repo.git/': The requested URL returned error: 403

此时本地.git/config文件为HTTPS格式:

[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
    ignorecase = true
    precomposeunicode = true
[remote "upstream"]
    url = https://github.com/uName/repo.git
    fetch = +refs/heads/*:refs/remotes/upstream/*
[remote "origin"]
    url = https://github.com/uName/repo.git
    fetch = +refs/heads/*:refs/remotes/origin/*

SSH方式尝试后的问题

将.git/config中的仓库地址改为ssh://git@github.com/uName/repo.git格式后,再次推送仍报错:

git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

已完成的排查动作

  • 确认GitHub账户中已正确添加~/.ssh/id_rsa.pub的公钥
  • 删除Mac钥匙串中所有与github.com相关的密钥条目
  • 尝试过主流论坛的常规解决方案,耗时两天未解决
  • 当前执行git remote -v显示仓库地址为HTTPS格式

小众排查建议

  • 核对本地Git全局配置的用户名/邮箱:执行git config --global user.name和git config --global user.email,确认与GitHub个人资料中的信息完全匹配;若GitHub设置了隐私邮箱,需确保使用的是平台分配的noreply邮箱
  • 清理Git凭据助手残留的旧凭据:执行git config --global credential.helper查看当前使用的凭据助手,若为osxkeychain,可执行git credential-osxkeychain erase https://github.com强制清除对应凭据;若为其他类型(如cache),可执行git credential-cache exit或重启终端
  • 解决多SSH密钥冲突:若本地存在多个SSH密钥(用于不同Git服务),需创建~/.ssh/config文件并指定GitHub对应的密钥,示例配置:
    Host github.com
      User git
      IdentityFile ~/.ssh/id_rsa
      IdentitiesOnly yes
    
    配置完成后执行ssh -T git@github.com测试SSH连接是否成功
  • 检查仓库分支保护规则:确认目标仓库的master分支是否开启了分支保护(如要求通过PR合并、仅特定用户可推送),即使拥有仓库权限,也可能被分支规则限制直接推送
  • 排除代理/VPN干扰:若当前网络使用了代理或VPN,尝试关闭后再测试推送;同时检查Git的代理配置,执行git config --global http.proxy和git config --global https.proxy,若有设置则清空后重试
  • 验证GitHub App权限:若账户是通过GitHub App授权访问仓库,需确认该App是否拥有推送代码的权限,以及是否被管理员撤销或限制
  • 检查SSH密钥权限:确保~/.ssh/id_rsa权限为600,~/.ssh/id_rsa.pub权限为644,~/.ssh目录权限为700,权限过宽会导致SSH拒绝使用该密钥
  • 确认仓库正确性:检查当前克隆的仓库是否为你拥有权限的目标仓库,注意GitHub仓库名不区分大小写,但本地Git可能存在大小写识别差异,避免克隆了同名的其他仓库

内容的提问来源于stack exchange,提问作者cambrianColor

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 11:35:08