git push报错-- failed to push some refs to ‘XXXXXXXX’,最终发现原因是refusing to merge unrelated histories

git push 报错 failed to push some refs to ‘XXXXXXXX’,最终发现原因是refusing to merge unrelated histories

在使用git第一次推送新项目代码到公司的git仓库的时候,报错

$ git push origin master
To git.XXXXXXXXl.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to '[email protected]'
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.

然后我尝试拉取也是失败

$ git pull origin master
From git.XXXXXXXXXX
 * branch            master     -> FETCH_HEAD
fatal: refusing to merge unrelated histories

分析

刚开始搜索push的报错信息,相关帖子都没说到点上,最后copy信息出来翻译之后查看(我英语比较菜,英语好的可以直接看信息)说是版本落后,更新被拒绝,提示先pull一下,尝试pull也失败,同样翻译失败原因,并搜索相关文章查看,定位到问题,原来是远程仓库没有和本地仓库建立连接。
自己分析之后猜测:我出现这个问题的原因应该是在管理后台建立好仓库之后还没有建立主干分支的时候,我关联了远程仓库,之后老大建立了主干提交,然后我这里的仓库就没有和主干建立关联了,所以报错。
确定问题之后就好办了,有两种解决方式

  1. 重新从git上拉代码到新的目录,然后把自己的工程添加到新目录里,然后提交修改(我这里主干只有readme,工程代码都在我这还没提交,所以可以这么搞,如果比较混乱不建议这样做)
  2. 强制合并 git pull origin master --allow-unrelated-histories

解决步骤

这里使用第二种方式,步骤:

  1. 执行强制合并git pull origin master --allow-unrelated-histories 执行结果:
$ git pull origin master --allow-unrelated-histories
From git.XXXXXXXXXXXXXXXXXl
* branch            master     -> FETCH_HEAD
CONFLICT (add/add): Merge conflict in README.md
Auto-merging README.md
Automatic merge failed; fix conflicts and then commit the result.
  1. 解决冲突。因为主干上只有readme,但是和我工程中的readme冲突了,看上面的提示说先解决冲突再提交结果。解决冲突就把那些===== 《《《删掉就好了
  2. 提交结果git commit -m "解决冲突"
  3. 就可以愉快的推送代码了。git push origin master。结果成功
$ git push origin master
Enumerating objects: 64, done.
Counting objects: 100% (64/64), done.
Delta compression using up to 4 threads
Compressing objects: 100% (46/46), done.
Writing objects: 100% (62/62), 23.95 KiB | 2.18 MiB/s, done.
Total 62 (delta 5), reused 0 (delta 0)
To git.XXXXXXXXXXXXXXX
   16XXXX0..4cXXXXXe  master -> master

git push报错-- failed to push some refs to ‘XXXXXXXX’,最终发现原因是refusing to merge unrelated histories_第1张图片

小记

git命令比较多,不熟悉的话使用命令行会有很多情况,但是提示还是很友好的,所有出了问题先看下提示信息,看不懂就翻译,不建议直接就复制报错去搜,容易误导。

参考:https://blog.csdn.net/u012145252/article/details/80628451

你可能感兴趣的:(版本控制,git)