如何解决Gitee提交pr冲突

下面以system_cpu_probe冲突为例,介绍解决冲突的流程。
在这里插入图片描述
在此之前,建议了解一下git基本命令的原理。
图片源自网络可参考:
git工作原理及提交规范【干货】
Git 原理入门

解决方案:本地手动同步远程分支,解决冲突

因为创建、合并和删除分支非常快,所以Git鼓励你使用分支完成某个任务,合并后再删掉分支,这和直接在master分支上工作效果是一样的,但过程更安全。

以下步骤,均在本地master分支(即提交Pr的分支)进行。
当然,为安全起见,可以新开一个分支,在此之上进行操作。但后续需要合并(merge)到master分支,再提交,详情可参考廖雪峰的blog:解决冲突

  1. 如果只有一个commit,直接跳到第2步。否则:先尝试合并所有提交的commit为一个,如果不行,查看日志,回退到最初的版本。回退前,保存好后面提交commit的相关改动。
[root@localhost A-Ops]# git log

[root@localhost A-Ops]# git reset --hard <最初版本的commit_id>
  1. 尝试同步远程分支
[root@localhost A-Ops]# git fetch upstream

[root@localhost A-Ops]# git rebase upstream/master
Auto-merging gala-gopher/src/probes/system_infos.probe/system_cpu.c
CONFLICT (content): Merge conflict in gala-gopher/src/probes/system_infos.probe/system_cpu.c
error: could not apply 0d8ab09... system cpu probe: add 2 metrics, and make some modifications
Resolve all conflicts manually, mark them as resolved with
"git add/rm <conflicted_files>", then run "git rebase --continue".
You can instead skip this commit: run "git rebase --skip".
To abort and get back to the state before "git rebase", run "git rebase --abort".
Could not apply 0d8ab09... system cpu probe: add 2 metrics, and make some modifications

可以看到,在与远程分支自动合并gala-gopher/src/probes/system_infos.probe/system_cpu.c时,发生冲突,需要手动解决。在此之后,还需要执行git add/rm <conflicted_files>git rebase --continue
如果中途遇到任何rebase误操作,可以执行git rebase --bort终止这次rebase。

  1. 进入冲突的文件,手动修复冲突
[root@localhost A-Ops]# vim gala-gopher/src/probes/system_infos.probe/system_cpu.c

在这里插入图片描述

  1. 接受远程仓库的更新。把原commit代码和无关语句删去,保存退出
    在这里插入图片描述
  2. 对修改后的文件执行add操作
[root@localhost A-Ops]# git add gala-gopher/src/probes/system_infos.probe/system_cpu.c
  1. 继续rebase操作。此时会弹出一个编辑页面,保存退出即可。执行成功后如下所示。
[root@localhost A-Ops]# git rebase --continue
[detached HEAD 0eafd53] system cpu probe: add 2 metrics, and make some modifications
 Committer: fenghaiyue <root@localhost.localdomain>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:

    git config --global user.name "Your Name"
    git config --global user.email you@example.com

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 3 files changed, 82 insertions(+), 15 deletions(-)
Successfully rebased and updated refs/heads/master.

在这里插入图片描述

  1. 可以按第6步的提示,显式地设置自己的用户名和邮箱,以免出现cla-no
[root@localhost A-Ops]# git config --global user.name <gitee_id>
[root@localhost A-Ops]# git config --global user.email <your_email>
  1. 提交自己的修改
[root@localhost A-Ops]# git commit --amend
[master 4352013] system cpu probe: add 2 metrics, and make some modifications
 Author: fenghaiyue <root@localhost.localdomain>
 Date: Wed Jul 20 12:04:22 2022 +0800
 3 files changed, 82 insertions(+), 15 deletions(-)
  1. 强制推送到远程仓库
[root@localhost A-Ops]# git push -f
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值