演示场景
虽然每次合并代码前会先把分支更新到最新,但是在你pull后到push前这段时间,可能其它小伙伴又push了,那么你的分支就不是最新的了
在push的时候就会失败,比如遇到这种提示信息:
To gitee.com:qzcsbj/pytest_apiautotest.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'gitee.com:qzcsbj/pytest_apiautotest.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.
本篇我们演示这种场景,并用merge解决冲突。
数据准备
重新克隆
日志
远程分支qzcsbj.txt内容
commit id
其他人提交
模拟其他人对master做了提交:直接gitee上修改文件并提交
新的commit id
本地提交
本地分支修改qzcsbj.txt内容为:
先提交到本地仓库
日志
推送到远程仓库,报错
To gitee.com:qzcsbj/pytest_apiautotest.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'gitee.com:qzcsbj/pytest_apiautotest.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.
大概意思是:远程仓库别人推送的内容,我们本地没有,也就是我们本地master不是最新的
解决冲突
将远程仓库的master分支下载到本地当前branch中
git fetch origin master
可以查看本地分支和fetch的分支差异:git diff master FETCH_HEAD,我们这里只有qzcsbj.txt的内容有差异
git diff master origin/master
进行合并:git merge origin/master
或者:git merge fetch_head
提示做了自动合并,但是自动合并失败了;另外,下面master -> origin的颜色变了
查看文件内容
合并内容为:
再次推送
To gitee.com:qzcsbj/pytest_apiautotest.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'gitee.com:qzcsbj/pytest_apiautotest.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.
大概意思是:当前分支落后远程分支
问题原因是:刚刚我们已经和远程分支合并了,但是没提交到本地仓库,所以执行下面操作:
git status
git add .
git commit -m "xxx"
git push
push成功后,远程分支内容
新的commit id
日志:下面c545426是模拟别人提交的id
查看分支合并图
git log --graph
git log --graph --oneline
__EOF__
本文作者:持之以恒(韧)
关于博主:擅长性能、全链路、自动化、企业级自动化持续集成(DevTestOps)、测开等