0
点赞
收藏
分享

微信扫一扫

Git必知必会基础(13):远程冲突(conflicts)解决--rebase

 

 

演示场景

虽然每次合并代码前会先把分支更新到最新,但是在你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.

 

本篇我们演示这种场景,并用rebase解决冲突。

git pull --rebase = git fetch + git rebase

 

数据准备

重新克隆

Git必知必会基础(13):远程冲突(conflicts)解决--rebase_sed

  

日志

Git必知必会基础(13):远程冲突(conflicts)解决--rebase_sed_02

 

远程分支qzcsbj.txt内容

Git必知必会基础(13):远程冲突(conflicts)解决--rebase_sed_03

 

commit id

Git必知必会基础(13):远程冲突(conflicts)解决--rebase_远程分支_04

 

其他人提交

模拟其他人对master做了提交:直接gitee上修改文件并提交

Git必知必会基础(13):远程冲突(conflicts)解决--rebase_远程分支_05

 

新的commit id

Git必知必会基础(13):远程冲突(conflicts)解决--rebase_git_06

 

本地提交

本地分支修改qzcsbj.txt内容为:

Git必知必会基础(13):远程冲突(conflicts)解决--rebase_git_07

 

添加到暂存区

Git必知必会基础(13):远程冲突(conflicts)解决--rebase_远程分支_08

 

Git必知必会基础(13):远程冲突(conflicts)解决--rebase_sed_09

 

推送到远程仓库,报错

Git必知必会基础(13):远程冲突(conflicts)解决--rebase_远程分支_10

 

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.

 

大概意思是:远程仓库别人推送的内容,我们本地没有

 

日志

Git必知必会基础(13):远程冲突(conflicts)解决--rebase_git_11

 

 

解决冲突

拉取最新master分支,并rebase到该分支

git pull --rebase origin master

下面做了自动合并;当前在远程分支版本516f1a4

Git必知必会基础(13):远程冲突(conflicts)解决--rebase_git_12

 

日志:git log中少了5344be2

Git必知必会基础(13):远程冲突(conflicts)解决--rebase_git_13

 

git status

Git必知必会基础(13):远程冲突(conflicts)解决--rebase_sed_14

 

查看文件内容

Git必知必会基础(13):远程冲突(conflicts)解决--rebase_远程分支_15

 

内容修改为:

Git必知必会基础(13):远程冲突(conflicts)解决--rebase_git_16

 

状态和日志

Git必知必会基础(13):远程冲突(conflicts)解决--rebase_git_17

 

添加到暂存区

Git必知必会基础(13):远程冲突(conflicts)解决--rebase_sed_18

 

日志

Git必知必会基础(13):远程冲突(conflicts)解决--rebase_git_19

 

git rebase --continue,提示不能提交暂存的修改

Git必知必会基础(13):远程冲突(conflicts)解决--rebase_远程分支_20

 

error: Terminal is dumb, but EDITOR unset,意思是终端无响应,未设置EDITOR

https://stackoverflow.com/questions/64276769/git-rebase-continue-without-opening-the-editor/64350016#64350016

 

方法一:换用git bash

说明,方法一的截图和前面非同一个demo,这里仅仅为了演示;续接上面数据,我们会用方法二继续演示

Git必知必会基础(13):远程冲突(conflicts)解决--rebase_sed_21

执行git rebase --continue后,直接进入命令模式,显示类似如下的内容,也就是说会让修改提交信息

Git必知必会基础(13):远程冲突(conflicts)解决--rebase_sed_22

输入i进入编辑模式,修改log信息

Git必知必会基础(13):远程冲突(conflicts)解决--rebase_git_23

 

修改完成后,esc退出编辑模式,:wq保存退出,然后会显示如下信息,说明生成了一个新的commit id(7e80ee6)、变基成功并更新refs/heads/master

Git必知必会基础(13):远程冲突(conflicts)解决--rebase_git_24

此时直接push就可以成功

 

下面是日志

Git必知必会基础(13):远程冲突(conflicts)解决--rebase_git_25

 

Git必知必会基础(13):远程冲突(conflicts)解决--rebase_远程分支_26

 

 

方法二:执行commit

如果终端未设置EDITOR,我们也可以自己commit 

commit,颜色变成白色了,生成了一个新的commit id

Git必知必会基础(13):远程冲突(conflicts)解决--rebase_git_27

 

状态和日志:当前 master分支在516f1a4,HEAD在30aa53a

Git必知必会基础(13):远程冲突(conflicts)解决--rebase_远程分支_28

 

git rebase --continue,master rebase到HEAD

Git必知必会基础(13):远程冲突(conflicts)解决--rebase_git_29

 

Git必知必会基础(13):远程冲突(conflicts)解决--rebase_git_30

 

状态、日志:git log中,少了5344be2

Git必知必会基础(13):远程冲突(conflicts)解决--rebase_远程分支_31

 

git push

Git必知必会基础(13):远程冲突(conflicts)解决--rebase_git_32

 

push成功后,远程分支内容

Git必知必会基础(13):远程冲突(conflicts)解决--rebase_sed_33

 

commit id

Git必知必会基础(13):远程冲突(conflicts)解决--rebase_git_34

 

查看分支合并图

git log --graph,是一条直线

Git必知必会基础(13):远程冲突(conflicts)解决--rebase_远程分支_35

 

Git必知必会基础(13):远程冲突(conflicts)解决--rebase_远程分支_36

 

git log --graph --oneline

Git必知必会基础(13):远程冲突(conflicts)解决--rebase_sed_37

Git必知必会基础(13):远程冲突(conflicts)解决--rebase_git_38

 

【bak】

 

__EOF__


本文作者:持之以恒(韧)
关于博主:擅长性能、全链路、自动化、企业级自动化持续集成(DevTestOps)、测开等


举报

相关推荐

0 条评论