Git:报错 ! [rejected] master -> master (non-fast-forward)

该错误引用下面这篇博客有解释:https://blog.csdn.net/weixin_41287260/article/details/89742151
这里不再做赘述,主要就是远端仓库和本地仓库存在冲突

按照上面博客提供的解决方案,不能解决该问题,会在合并时报出新的错误

$ git merge origin FETCH_HEAD
warning: refname 'FETCH_HEAD' is ambiguous.
fatal: refusing to merge unrelated histories

换个思路,远端仓库是否为新建仓库?是!至少笔者是遇到的这种情况

那就直接忽略远端和近端的差异直接拉取代码再提交

$ git pull origin master --allow-unrelated-histories
From https://github.com/XXX/XXX
 * branch            master     -> FETCH_HEAD
Merge made by the 'recursive' strategy.
 .gitignore | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
 create mode 100644 .gitignore

拉取成功了,代码成功合并。
再次提交代码:

$ git push origin master
Enumerating objects: 42, done.
Counting objects: 100% (42/42), done.
Delta compression using up to 8 threads
Compressing objects: 100% (40/40), done.
Writing objects: 100% (41/41), 31.08 KiB | 1.20 MiB/s, done.
Total 41 (delta 17), reused 0 (delta 0)
remote: Resolving deltas: 100% (17/17), done.
To https://github.com/XXX/XXX.git
   153cc3f..c74da89  master -> master

提交成功。

简单的情况解决了,我们再看一下复杂的情况
远端和本地都有重要的修改。

到这里作者发现,参考的链接里提供了安全的解决方案,请参考:https://blog.csdn.net/weixin_41287260/article/details/89742151

如遇到冲突,谨慎解决冲突后再尝试合并。

你可能感兴趣的:(日常工具)