gerrit出现Merge Conflict,小白解决心路历程

错误的解决之路

gerrit上出现Merge Conflict时

在IDEA进行git pull时,会出现冲突如下所示,用HEAD>>>标出来

error: could not apply ec2a685ab...
hint: Resolve all conflicts manually, mark them as resolved with
hint: "git add/rm <conflicted_files>", then run "git rebase --continue".
hint: You can instead skip this commit: run "git rebase --skip".
hint: To abort and get back to the state before "git rebase", run "git rebase --abort".

此时,我就慌了啊,怎么解决呢,那我先去删除我的冲突地方,即HEAD<<< >>>的地方,保留上面的,上面是人家写的,我的是下面的

然后我进行git stash save "XXX",把我现在修改的进行保存起来,进行之前的类似的提交操作,结果出现如下报错

xxxx.java: needs merge

没办法我就不进行更新了,那老子直接进行git add .

吆西,成功了

鬼知道我怎么这么迷恋git stash save "xxxx",没想到成功了(报错的源头,因为我居然在no branch分支上,说明保存错了啊)

Saved working directory and index state On (no branch): name

我进行git pull,错误升级,变成这样了

You are not currently on a branch.
Please specify which branch you want to rebase against.
See git-pull(1) for details.

行吧,git pull不行对吧,老子进行git stash pop stash@{0},没想到居然冒出这玩意,错误加倍

interactive rebase in progress; onto 12beffbb8
Last command done (1 command done):
......
(use "git rebase --edit-todo" to view and edit)
You are currently editing a commit while rebasing branch 'dev_ecloud' on '12beffbb8'.
  (use "git commit --amend" to amend the current commit)
  (use "git rebase --continue" once you are satisfied with your changes)

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
.......

告诉我当前正在编辑的东西,如果提交的将会覆盖这个“12beffbb8”。使用git rebase进行代码覆盖,但是上次提交的进程还没有完成导致的。

老子不管三七二十一,先给它push上去,看看能咋地,结果成功了。但是我之前的任务被拉取的代码给覆盖,我大呼奇迹,简直魔术师转世。老子的代码都丢了,又得去再拷贝一遍。

我再看看我在哪个分支,用git branch -a,显示如下:

* (no branch, rebasing dev_ecloud)
  dev_ecloud
  master

老子甚至百度了,人家解决这种问题,就是创建临时分支,然后拉取dev分支,将临时分支合并到dev分支,删除临时分支

//先用git status查看现在临时分支的名字
git status

interactive rebase in progress; onto 12beffbb8
Last command done (1 command done):
   pick .....
  (use "git rebase --edit-todo" to view and edit)
You are currently editing a commit while rebasing branch 'dev_ecloud' on '12beffbb8'.
  (use "git commit --amend" to amend the current commit)
  (use "git rebase --continue" once you are satisfied with your changes)

nothing to commit, working tree clean

//创建临时分支
git branch temp 12beffbb8

//拉取dev分支
git checkout dev_ecloud

Already on 'dev_ecloud'
Your branch and 'origin/dev_ecloud' have diverged,
and have 3 and 1 different commits each, respectively.
  (use "git pull" to merge the remote branch into yours)

//拉取远程dev分支
git checkout remotes/origin/dev_ecloud

//进行将临时分支合并到拉取的分支上
git merge temp 

Auto-merging xxx.java
CONFLICT (content): Merge conflict in xxx.java
Automatic merge failed; fix conflicts and then commit the result.

//看不懂上面,老子觉得大事不好,就删除临时分支
git branch -D temp

那我现在再看看git status吧,按照提示进行git add .,显示如下

interactive rebase in progress; onto 12beffbb8
Last command done (1 command done):
   pick ec2a685ab xxxxx
  (use "git rebase --edit-todo" to view and edit)

All conflicts fixed but you are still merging.
  (use "git commit" to conclude merge)

Changes to be committed:

然后我觉得按照提示吧,用git rebase --edit-todo,结果是编辑代码的

到这里才是正确的解决方案

我就换成git rebase --continue,这个用来解决合并冲突

注意注意

执行完git pull --rebase之后如果有合并冲突,使用以下三种方式处理这些冲突:

git rebase --abort 会放弃合并,回到rebase操作之前的状态,之前的提交的不会丢弃;

git rebase --skip 则会将引起冲突的commits丢弃掉(慎用!!);

git rebase --continue 合并冲突,结合"git add 文件"命令一起用与修复冲突,提示开发者,一步一步地有没有解决冲突。(fix conflicts and then run "git rebase --continue")

具体的细化就是这个

对上述冲突的处理

1、使用 $git rebase --abort

执行之后,本地内容会回到提交之间的状态,也就是回到以前提交但没有pull是的状态,简单来说就是撤销rebase。

2、使用 $git rebase --skip

git rebase --skip 引起冲突的commits会被丢弃,对于本文应用的例子来说开发者A对c.sh文件的commit无效,开发者A自己修改的部分全部无效,因此,在使用skip时请慎重。

执行:$ vim c.sh

查看本地c.sh文件提交内容,展示如下图所示,执行语句之后开发者A的修改无效。

3、使用 $git rebase --continue

执行完$git pull --rebase 之后,本地如果产生冲突,手动解决冲突之后,用"git add"命令去更新这些内容的索引(index),然后只要执行:

$ git rebase --continue 就可以线性的连接本地分支与远程分支,无误之后就回退出,回到主分支上。
注意:一般情况下,修改后检查没问题,使用rebase continue来合并冲突。

然后我就利用git rebase --continue,就显示成功了。显示结果如下

//解决完成冲突,合并冲突,没有问题的话就合并到主分支
git rebase --continue

[detached HEAD da99a7669]xxxxx
Successfully rebased and updated refs/heads/dev_ecloud.

然后就继续别的操作就完成了,主要是上面这个命令。

还有一点补充:

在gerrit上因为这个任务出现冲突,导致在这之后的其他工程也出会出现Merge Conflict

怎么解决呢?

然后打开你的gerrit上的Rebase,勾选Change parent revision,点击Rebase,你就解决完毕了。

为啥这就行了呢,因为这个rebase就相当于基于现在最新的dev分支拉取基线,我感觉很像咱们再IDEA命令行中写的git rebase --continue。

这就解决了,这是我的心路历程,如果觉得不错就点个赞吧!

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值