git (non-fast-forward) 错误粗暴处理


问题描述

今天更新项目的时候, 遇到 (non-fast-forward) 错误.

$ git push origin master
Username for 'https://github.com': itaken
Password for 'https://itaken@github.com':
To https://github.com/itaken/vue2-todolist.git
 ! [rejected]        master -> master (non-fast-forward)
error: 无法推送一些引用到 'https://github.com/itaken/vue2-todolist.git'
提示:更新被拒绝,因为您当前分支的最新提交落后于其对应的远程分支。
提示:再次推送前,先与远程变更合并(如 'git pull ...')。详见
提示:'git push --help' 中的 'Note about fast-forwards' 小节。

因为github上的项目是新建的,新增了README.md文件,导致git 版本不一致, 无法更新.

$ git pull origin master -a                                             129 ↵
fatal: unable to access 'https://github.com/itaken/vue2-todolist.git/': Empty reply from server

$ git pull origin master -f                                               1 ↵
来自 https://github.com/itaken/vue2-todolist
 * branch            master     -> FETCH_HEAD
fatal: 拒绝合并无关的历史

解决方法

最简单粗暴的解决方案就是: --force强更. ^1

$ git push --force origin master                                        130 ↵
Username for 'https://github.com': itaken
Password for 'https://itaken@github.com':
对象计数中: 83, 完成.
Delta compression using up to 4 threads.
压缩对象中: 100% (78/78), 完成.
写入对象中: 100% (83/83), 73.34 KiB | 0 bytes/s, 完成.
Total 83 (delta 15), reused 0 (delta 0)
remote: Resolving deltas: 100% (15/15), done.
To https://github.com/itaken/vue2-todolist.git
 + 99646e9...9225a2a master -> master (forced update)

Git force pull to overwrite local files

$ git fetch --all
$ git reset --hard origin/master
$ git pull origin master

参考文档


Author: Itaken
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint polocy. If reproduced, please indicate source Itaken !
  TOC目录