[GIT]解决:failed to push some refs to ...(经验复盘)

发布时间 2023-07-13 18:17:52作者: 千千寰宇

1 问题描述

johnnyzen@XXDSSS MINGW64 /e/source_code/BigData/bdp_common_data_service (develop)
$ git push
To gitlab-bigdata.johnnyzen.cn:platform-software/bigdata/bdp_common_data_service.git
 ! [rejected]        develop -> develop (non-fast-forward)
error: failed to push some refs to 'gitlab-bigdata.johnnyzen.cn:platform-software/bigdata/bdp_common_data_service.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.

2 问题分析

  • Step0 小伙伴A、B:同步【远程仓库】的代码至【本地仓库】
git pull
  • Step1.1 小伙伴A:修改【本地】的代码,并提交至【本地仓库】,再推送至【远程仓库】
修改本地代码
git add *

git pull

git commit -m "xxxx"

git push
  • Step1.2 小伙伴B:修改【本地】的代码,并提交至【本地仓库】,但未推送至【远程仓库】
修改本地代码

git add *

git commit -m "xxxxx"
  • Step2 小伙伴B:再次修改/新增/删除了【本地】的代码,并尝试推送先前提交的【本地仓库】代码至【远程仓库】
再次修改/新增/删除了【本地】的代码

git push
(此时,出现了本错误)
To gitlab-bigdata.johnnyzen.cn:platform-software/bigdata/bdp_common_data_service.git
 ! [rejected]        develop -> develop (non-fast-forward)
error: failed to push some refs to 'gitlab-bigdata.johnnyzen.cn:platform-software/bigdata/bdp_common_data_service.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.

3 解决方法

此问题有很多解决方法,答主仅提供了一种我个人的解决方式

解决方法1

  • Step3 检查哪些代码是Step2过程中修改的代码

那么这个时候,小伙伴B可以先git pull尝试同步一下远程仓库的代码,以检测是哪些文件出现了冲突。

git pull

  • Step4 将上述变更文件拷贝到源码工程外,并使得Step2过程无任何变更

  • Step5 同步远程仓库的代码,并在本地解决可能出现的冲突

git pull

[并解决可能出现的冲突]

  • Step6 推送代码至【远程仓库】
git push

  • Step7 还原Step4中备份的变更代码至源码工程内

X 参考文献