Git Push 失败常见原因排查
Git push 失败是开发中常见问题,通常由权限、配置或内容冲突引起。以下是 8 种常见原因,每种都包括原因分析、如何识别(通过错误消息)和解决步骤。排查时,建议依次检查这些点,确保问题高效解决。
1. 权限不足 (Permission Denied)
- 原因:用户对远程仓库没有写入权限,常见于 SSH 密钥无效或账户权限不足。
总结了 Git push 失败的八种常见原因及解决方案。包括权限不足导致的拒绝访问、分支保护禁止直接推送、本地与远程内容冲突未解决、本地分支落后于远程、远程仓库 URL 配置错误、网络连接或代理问题、认证凭证失效以及文件超过平台大小限制。针对每种情况提供了具体的错误识别方法和解决步骤,如检查 SSH 密钥、使用 Pull Request 合并代码、解决合并冲突、更新远程 URL、配置网络代理、更新凭证或使用 Git LFS 等。建议定期拉取更新并使用 dry-run 测试推送以预防问题。
Git push 失败是开发中常见问题,通常由权限、配置或内容冲突引起。以下是 8 种常见原因,每种都包括原因分析、如何识别(通过错误消息)和解决步骤。排查时,建议依次检查这些点,确保问题高效解决。
remote: Permission to user/repo.git denied to user. 或 fatal: unable to access 'https://github.com/user/repo.git/': The requested URL returned error: 403。ssh -T [email protected] 测试连接。git config --global user.name 和 git config --global user.email 验证账户。git credential-manager reject(Windows)或 git config --global credential.helper cache(Linux/macOS)重新输入密码。main 或 master)被设置为保护分支,禁止直接推送,常见于 GitHub 或 GitLab。remote: error: GH006: Protected branch update failed for refs/heads/main. 或 remote: You are not allowed to force push code to a protected branch on this project.。git push --force-with-lease(谨慎使用)。! [rejected] main -> main (fetch first) 或 error: failed to push some refs to 'origin',提示先运行 git pull。git pull origin <branch>(将 <branch> 替换为实际分支名)。<<<<<<< HEAD)。git add . 后 git commit -m "Resolved conflicts"。git push origin <branch>。! [rejected] main -> main (non-fast-forward) 或 Updates were rejected because the remote contains work that you do not have locally.。git pull --rebase origin <branch>(推荐 rebase 避免额外合并提交)。git push origin <branch>。fatal: repository 'https://github.com/wrong/repo.git' not found 或 remote: Repository not found.。git remote -v。git remote set-url origin https://github.com/correct/repo.git。git fetch origin 验证是否成功。fatal: unable to access 'https://github.com/user/repo.git/': Failed to connect to github.com port 443: Timed out 或 Connection reset by peer。ping github.com 测试连通性。git config --global --unset http.proxy。git push。remote: Invalid username or password. 或 fatal: Authentication failed for 'https://github.com/user/repo.git/'。git config --global credential.helper store 保存新凭证。remote: error: File largefile.bin is 120.00 MB; this exceeds GitHub's file size limit of 100.00 MB 或 remote: fatal: pack exceeds maximum allowed size。git ls-files -s | awk '{print $2}' | xargs -I {} git cat-file -s {} 列出大文件。git rm --cached largefile.bin 删除缓存。git lfs track "*.bin"。git gc --prune=now 优化仓库。git push -v 获取详细输出。git status 和 git log --oneline 查看分支状态。git fetch 或 ssh -T [email protected] 排除网络问题。git pull 保持本地更新。git push --dry-run 测试推送前检查。如果问题持续,提供完整错误消息能帮助进一步诊断。

微信公众号「极客日志」,在微信中扫描左侧二维码关注。展示文案:极客日志 zeeklog
将字符串编码和解码为其 Base64 格式表示形式即可。 在线工具,Base64 字符串编码/解码在线工具,online
将字符串、文件或图像转换为其 Base64 表示形式。 在线工具,Base64 文件转换器在线工具,online
将 Markdown(GFM)转为 HTML 片段,浏览器内 marked 解析;与 HTML转Markdown 互为补充。 在线工具,Markdown转HTML在线工具,online
将 HTML 片段转为 GitHub Flavored Markdown,支持标题、列表、链接、代码块与表格等;浏览器内处理,可链接预填。 在线工具,HTML转Markdown在线工具,online
通过删除不必要的空白来缩小和压缩JSON。 在线工具,JSON 压缩在线工具,online
将JSON字符串修饰为友好的可读格式。 在线工具,JSON美化和格式化在线工具,online