无法向GitHub仓库推送代码,git push认证失败求助
解决Git Push到GitHub失败(输入用户名/令牌仍无效)的实用方案
检查远程仓库URL正确性
执行git remote -v查看当前远程地址,确认是HTTPS格式且拼写无误(比如仓库名、用户名没打错)。如果是SSH格式,需改用SSH密钥验证,而非用户名/令牌。确保个人访问令牌(PAT)权限足够
前往GitHub账号设置→开发者设置→个人访问令牌,重新生成令牌时必须勾选repo权限(私有仓库需勾选完整repo,公开仓库勾选public_repo),令牌生成后立即复制使用(无法再次查看)。清除Git缓存的旧凭据
Git可能缓存了失效的密码/令牌,导致新凭据无法生效:- Windows:
git config --global credential.helper wincred git credential-manager erase https://github.com - macOS:
git config --global credential.helper osxkeychain git credential-osxkeychain erase https://github.com - Linux:
git config --global credential.helper store # 编辑~/.git-credentials文件,删除GitHub相关行
- Windows:
直接在远程URL中嵌入令牌(临时测试)
修改远程仓库URL,将令牌直接嵌入:git remote set-url origin https://<你的GitHub用户名>:<你的PAT>@github.com/<你的GitHub用户名>/<仓库名>.git执行完后再尝试
git push -u origin main。核对Git全局配置的用户名/邮箱
执行以下命令确认配置与GitHub账号一致:git config --global user.name git config --global user.email若不一致,用
git config --global user.name "你的用户名"和git config --global user.email "你的邮箱"修改。排查网络与代理问题
- 若使用代理,先关闭Git的代理配置:
git config --global --unset http.proxy git config --global --unset https.proxy - 切换网络环境(如手机热点),排除网络不稳定导致的推送失败。
- 若使用代理,先关闭Git的代理配置:
内容的提问来源于stack exchange,提问作者jeremyt123
相关产品推荐
相关产品推荐

