how to fix: error: failed to push some refs to

本文记录了解决Git在尝试将本地代码推送到GitHub远程仓库时遇到的非快速前进错误的过程。通过使用`git pull --rebase origin master`及后续的`git push origin master`命令,成功解决了冲突并完成了代码的推送。
最近在看GIT的相关教程,是廖雪峰老师的教程:[url]http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000[/url]
这个教程是从入门开始的,所以也还比较好懂。教程的前半部分都是在本地的操作,一切都是没有什么问题的,但是当要把本地的代码push到远程仓库的时候,就遇到了一个小的问题。因为我是在github上建立了一个learngit的仓库,并且建立的时候,顺便勾选建立readme.md文件,作为仓库的第一个提交。
下面是我的执行过程。
$git remote add origin git@github.com:xfxlch/learngit.git //添加远程主机名
$git push -u origin master //把本地的master分支代码push到远程仓库的master分支上。
这个时候得到了下面的错误。
[quote]
To git@github.com:xfxlch/learngit.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'git@github.com:xfxlch/learngit.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
[/quote]

然后我就用去执行了如下的命令:
$git pull origin master
执行完之后,什么都提示都没有,然后我再想去push代码的时候,仍然出现了之前的错误。

我很沮丧,就去Google了,然后找到了[url]http://stackoverflow.com/questions/24114676/git-error-failed-to-push-some-refs-to[/url]

[quote]
git pull --rebase origin master
git push origin master
[/quote]

完整的是这样的
[quote]
luchenghaodeMacBook-Air:learngit luchenghao$ git pull --rebase origin master
From github.com:xfxlch/learngit
* branch master -> FETCH_HEAD
First, rewinding head to replay your work on top of it...
Applying: add readme
Applying: udpated
Applying: small change
Applying: understand how stage works
Applying: check git status
Applying: add test.txt
luchenghaodeMacBook-Air:learngit luchenghao$ git push origin master
Counting objects: 18, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (14/14), done.
Writing objects: 100% (18/18), 1.48 KiB | 0 bytes/s, done.
Total 18 (delta 4), reused 0 (delta 0)
remote: Resolving deltas: 100% (4/4), done.
To git@github.com:xfxlch/learngit.git
4587b43..a5dca30 master -> master

[/quote]

他会replay之前的操作,就是相当于把我在本地的commit和在github上的commit做一次 彩排,到底哪个先哪个后,按时间顺序排一下,然后再push的时候,就可以正常提交了。

--EOF--
didi@MacBook-Pro-5 gen4_production_test % git branch * dev-ota-merge-0903 master didi@MacBook-Pro-5 gen4_production_test % git remote add origin https://git.xiaojukeji.com/voyager-test-group/product-test/gen4_production_test.git error: remote origin already exists. didi@MacBook-Pro-5 gen4_production_test % git push -u origin HEAD:dev_ota_test To git.xiaojukeji.com:voyager-test-group/product-test/gen4_production_test.git ! [rejected] HEAD -> dev_ota_test (fetch first) error: failed to push some refs to 'git.xiaojukeji.com:voyager-test-group/product-test/gen4_production_test.git' hint: Updates were rejected because the remote contains work that you do hint: not have locally. This is usually caused by another repository pushing hint: to the same ref. You may want to first integrate the remote changes hint: (e.g., 'git pull ...') before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details. didi@MacBook-Pro-5 gen4_production_test % git pull origin dev_ota_test remote: Enumerating objects: 196, done. remote: Counting objects: 100% (53/53), done. remote: Compressing objects: 100% (23/23), done. remote: Total 196 (delta 39), reused 41 (delta 30), pack-reused 143 Receiving objects: 100% (196/196), 87.48 KiB | 914.00 KiB/s, done. Resolving deltas: 100% (125/125), completed with 12 local objects. From git.xiaojukeji.com:voyager-test-group/product-test/gen4_production_test * branch dev_ota_test -> FETCH_HEAD * [new branch] dev_ota_test -> origin/dev_ota_test hint: You have divergent branches and need to specify how to reconcile them. hint: You can do so by running one of the following commands sometime before hint: your next pull: hint: hint: git config pull.rebase false # merge hint: git config pull.rebase true # rebase hint: git config pull.ff only # fast-forward only hint: hint: You can replace "git config" with "git config --global" to set a default hint: preference for all repositories. You can also pass --rebase, --no-rebase, hint: or --ff-only on the command line to override the configured default per hint: invocation. fatal: Need to specify how to reconcile divergent branches. didi@MacBook-Pro-5 gen4_production_test % git pull origin dev_ota_test --rebase error: cannot pull with rebase: You have unstaged changes. error: please commit or stash them. didi@MacBook-Pro-5 gen4_production_test % git commit On branch dev-ota-merge-0903 Your branch is ahead of 'origin/dev-ota-merge-0903' by 5 commits. (use "git push" to publish your local commits) Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: common_lib/web_lib/api_generate_ota_task.py no changes added to commit (use "git add" and/or "git commit -a") didi@MacBook-Pro-5 gen4_production_test % git add . didi@MacBook-Pro-5 gen4_production_test % git pull origin dev_ota_test --rebase error: cannot pull with rebase: Your index contains uncommitted changes. error: please commit or stash them. didi@MacBook-Pro-5 gen4_production_test % git commit -a Aborting commit due to empty commit message. didi@MacBook-Pro-5 gen4_production_test % git pull origin dev_ota_test --no-rebase From git.xiaojukeji.com:voyager-test-group/product-test/gen4_production_test * branch dev_ota_test -> FETCH_HEAD error: Your local changes to the following files would be overwritten by merge: common_lib/web_lib/api_generate_ota_task.py <stdin>:10: trailing whitespace. task_url, <stdin>:19: trailing whitespace. task_url, warning: 2 lines add whitespace errors. Merge with strategy ort failed. didi@MacBook-Pro-5 gen4_production_test % git add common_lib/web_lib/api_generate_ota_task.py didi@MacBook-Pro-5 gen4_production_test % git commit -m "feat: 添加 OTA 任务生成接口调用" [dev-ota-merge-0903 78aa496] feat: 添加 OTA 任务生成接口调用 1 file changed, 2 insertions(+), 2 deletions(-) didi@MacBook-Pro-5 gen4_production_test % git pull origin dev_ota_test --no-rebase From git.xiaojukeji.com:voyager-test-group/product-test/gen4_production_test * branch dev_ota_test -> FETCH_HEAD Auto-merging cases/ota_test/test_general_ota.py CONFLICT (content): Merge conflict in cases/ota_test/test_general_ota.py Auto-merging common_lib/web_lib/api_generate_ota_task.py CONFLICT (content): Merge conflict in common_lib/web_lib/api_generate_ota_task.py Automatic merge failed; fix conflicts and then commit the result. didi@MacBook-Pro-5 gen4_production_test % git pull origin dev_ota_test --no-rebase From git.xiaojukeji.com:voyager-test-group/product-test/gen4_production_test * branch dev_ota_test -> FETCH_HEAD Already up to date. didi@MacBook-Pro-5 gen4_production_test % git branch * dev-ota-merge-0903 master didi@MacBook-Pro-5 gen4_production_test % git push To git.xiaojukeji.com:voyager-test-group/product-test/gen4_production_test.git ! [rejected] dev-ota-merge-0903 -> dev-ota-merge-0903 (fetch first) error: failed to push some refs to 'git.xiaojukeji.com:voyager-test-group/product-test/gen4_production_test.git' hint: Updates were rejected because the remote contains work that you do hint: not have locally. This is usually caused by another repository pushing hint: to the same ref. You may want to first integrate the remote changes hint: (e.g., 'git pull ...') before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details. didi@MacBook-Pro-5 gen4_production_test % git branch * dev-ota-merge-0903 master didi@MacBook-Pro-5 gen4_production_test % git switch dev_ota_test branch 'dev_ota_test' set up to track 'origin/dev_ota_test'. Switched to a new branch 'dev_ota_test' didi@MacBook-Pro-5 gen4_production_test % git branch dev-ota-merge-0903 * dev_ota_test master didi@MacBook-Pro-5 gen4_production_test % git push -u origin dev_ota_test branch 'dev_ota_test' set up to track 'origin/dev_ota_test'. Everything up-to-date didi@MacBook-Pro-5 gen4_production_test % git branch dev-ota-merge-0903 * dev_ota_test master didi@MacBook-Pro-5 gen4_production_test % git add . didi@MacBook-Pro-5 gen4_production_test % git commit -m "提交机器人通知优化以及报文录制等" On branch dev_ota_test Your branch is up to date with 'origin/dev_ota_test'. nothing to commit, working tree clean didi@MacBook-Pro-5 gen4_production_test % git push Everything up-to-date didi@MacBook-Pro-5 gen4_production_test % git branch dev-ota-merge-0903 * dev_ota_test master didi@MacBook-Pro-5 gen4_production_test % git revert -m 1 HEAD [dev_ota_test 1564867] Revert "增加日志打印" 2 files changed, 1 insertion(+), 2 deletions(-) didi@MacBook-Pro-5 gen4_production_test % git log --oneline 1564867 (HEAD -> dev_ota_test) Revert "增加日志打印" a44efa5 (origin/dev_ota_test) 增加日志打印 501b72f 优化OTA前的磁盘空间清理 e5f7c7f voyager OTA前重启voy-ota,无需重启ACU df4c8a2 更新80101树莓派ip f0d3d5b voyager OTA前重启ACU 63f462d fix bug, 去掉注释,否则mdhil读不到version变量 09cceed Merge branch 'master' into dev-add_region_switch f53290a 解决空格引起的版本下载失败问题 a3d21f8 调整代码到lib dc7239f 增加改region代码 03a802a Merge branch 'master' into dev-add_region_switch b064d42 调试issue流程 e930019 切region自动化 097bfdd 服务器ip地址变更 b26a282 安装包文件增加匹配编译平台 bc1ee2a 调整升级方式为voy-ota-from-gift : a44efa5 (origin/dev_ota_test) 增加日志打印 501b72f 优化OTA前的磁盘空间清理 e5f7c7f voyager OTA前重启voy-ota,无需重启ACU df4c8a2 更新80101树莓派ip f0d3d5b voyager OTA前重启ACU 63f462d fix bug, 去掉注释,否则mdhil读不到version变量 09cceed Merge branch 'master' into dev-add_region_switch f53290a 解决空格引起的版本下载失败问题 a3d21f8 调整代码到lib dc7239f 增加改region代码 03a802a Merge branch 'master' into dev-add_region_switch b064d42 调试issue流程 e930019 切region自动化 097bfdd 服务器ip地址变更 b26a282 安装包文件增加匹配编译平台 bc1ee2a 调整升级方式为voy-ota-from-gift : a44efa5 (origin/dev_ota_test) 增加日志打印 501b72f 优化OTA前的磁盘空间清理 e5f7c7f voyager OTA前重启voy-ota,无需重启ACU df4c8a2 更新80101树莓派ip f0d3d5b voyager OTA前重启ACU 63f462d fix bug, 去掉注释,否则mdhil读不到version变量 09cceed Merge branch 'master' into dev-add_region_switch f53290a 解决空格引起的版本下载失败问题 a3d21f8 调整代码到lib dc7239f 增加改region代码 03a802a Merge branch 'master' into dev-add_region_switch b064d42 调试issue流程 e930019 切region自动化 097bfdd 服务器ip地址变更 b26a282 安装包文件增加匹配编译平台 bc1ee2a 调整升级方式为voy-ota-from-gift 9c086b3 kill-onboard-teleassis 413559e is_perf_test 立即落盘 2e55884 Change 80101 raspberry ip 2f6ca8a fenggaofeng:替换 apt install方式为 voy-ota-from-gift 4e13464 fenggaofeng:重启x86方式兼容A样和B样以及C样,不再需要单独切分支进行测试 c8526f6 Merge branch 'master' of git.xiaojukeji.com:voyager-test-group/product-test/gen4_production_test into dev_gen4_b_sp _reboot f601c35 fenggaofeng:lite_hil 碰撞节点增加perf flag,rtm、semantic、e2e增加启动动作。 bb9cf28 Merge branch 'master' of git.xiaojukeji.com:voyager-test-group/product-test/gen4_production_test into dev_gen4_b_sp: dc7239f 增加改region代码 03a802a Merge branch 'master' into dev-add_region_switch b064d42 调试issue流程 e930019 切region自动化 097bfdd 服务器ip地址变更 b26a282 安装包文件增加匹配编译平台 bc1ee2a 调整升级方式为voy-ota-from-gift 9c086b3 kill-onboard-teleassis 413559e is_perf_test 立即落盘 2e55884 Change 80101 raspberry ip 2f6ca8a fenggaofeng:替换 apt install方式为 voy-ota-from-gift 4e13464 fenggaofeng:重启x86方式兼容A样和B样以及C样,不再需要单独切分支进行测试 c8526f6 Merge branch 'master' of git.xiaojukeji.com:voyager-test-group/product-test/gen4_production_test into dev_gen4_b_sp_reboot f601c35 fenggaofeng:lite_hil 碰撞节点增加perf flag,rtm、semantic、e2e增加启动动作。 bb9cf28 Merge branch 'master' of git.xiaojukeji.com:voyager-test-group/product-test/gen4_production_test into dev_gen4_b_sp didi@MacBook-Pro-5 gen4_production_test % 帮我看一下是哪步将代码提交了我合并冲突之后原来的代码不见了想退回到原来的代码上
11-21
### 问题分析 在 Git 推送过程中,出现 `error: remote unpack failed` 和 `missing` 类似错误,通常与 Git 对象(如 blob、tree、commit)在远程仓库中缺失或损坏有关。这类错误常见于以下几种情况: - **Git 对象丢失**:远程仓库中缺少某个必要的 Git 对象(如 `Missing tree` 或 `Missing blob`)。 - **网络传输异常**:推送过程中对象未完整传输,导致远程仓库无法解析。 - **仓库损坏**:远程仓库的内部对象存储损坏,无法完成解包操作。 - **使用 `--no-thin` 参数**:在某些 Git 服务器(如 GitBlit)上,使用 `--no-thin` 可能导致推送失败,因为服务器期望接收到“薄”对象包[^3]。 ### 常见错误示例 ```bash error: remote unpack failed: error Missing tree eb96e0534e08fe512113b25a99d783278e04e8b1 error: remote unpack failed: error Missing blob 20529bef19e96bdf347674b9378c54e55817a0f6 error: failed to push some refs to 'ssh://git@server/path/to/repo.git' ``` ### 解决方案 #### 1. 检查本地仓库完整性 使用以下命令检查本地 Git 对象是否完整: ```bash git fsck ``` 若发现损坏对象,应尝试从其他完整副本中恢复。 #### 2. 使用 `git push --thin` 替代 `--no-thin` 某些 Git 服务器(如 GitBlit)对对象包格式有特定要求,避免使用 `--no-thin`,改用默认的 `--thin` 模式进行推送[^3]。 #### 3. 强制重新打包并推送 若本地仓库对象完整,但远程仓库存在缺失,可尝试重新打包并推送: ```bash git repack -d -l git push origin <branch-name> ``` 此操作将使用本地已有的增量链进行压缩,减少对远程对象的依赖。 #### 4. 清理远程仓库缓存 如果远程仓库使用的是 GitBlit、GitLab、Gitea 等托管平台,可尝试清理仓库缓存或执行 `git gc` 操作: ```bash git gc --aggressive ``` #### 5. 重建远程仓库 若远程仓库已损坏且无法修复,可考虑克隆远程仓库到本地,再重新初始化并推送: ```bash git clone --mirror ssh://git@server/path/to/repo.git cd repo.git git init --bare git fast-import < ../repo.git/objects/pack/pack-*.pack git push --mirror ssh://git@server/path/to/new-repo.git ``` #### 6. 避免使用 `--force` 推送 强制推送可能导致远程仓库对象丢失,尤其是在多人协作环境中。若必须使用 `--force`,建议先通知团队成员并备份远程仓库。 ### 预防措施 - 定期执行 `git fsck` 和 `git gc` 以维护仓库健康。 - 避免使用 `--no-thin` 推送至不支持该参数的 Git 服务器。 - 在多人协作环境中限制 `--force` 推送权限。 - 启用 Git 服务器的自动垃圾回收机制,防止对象丢失。 ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值