Skip to content

如何解决 PR 冲突问题

Bin Liu edited this page Sep 14, 2022 · 3 revisions

** 声明 ** 提交 PR 的人太多了,管理员来不及合并,所以很容易大家的 PR 因冲突而不得不修改,这里简单介绍一下如何在不创建新 PR 的前提下,解决冲突问题。

如果大家都在同一个代码基础上提交了 PR, 则别人的 PR 合并后,可能你的 PR 就冲突,因为你们修改了同一个地方。

这里我们来看一个具体的例子。

https://github.com/ituring/first-pr/pull/2378https://github.com/ituring/first-pr/pull/2379 ,这两个 PR 都修改了同一个地方,然后 2378 先被合并了,这时候, 2379 的页面会看到这种状态:

image

提示 index.html 冲突了,这时候需要解决这个冲突,再重新 push 就可以了。

拉取最新代码

假设当前在 pr-2 分支上,这时候需要如下操作:

$ git checkout gh-pages
Switched to branch 'gh-pages'
Your branch is up to date with 'origin/gh-pages'.

$ git remote add upstream https://github.com/ituring/first-pr
$ git fetch upstream
remote: Enumerating objects: 1, done.
remote: Counting objects: 100% (1/1), done.
remote: Total 1 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (1/1), done.
From https://github.com/ituring/first-pr
 * [new branch]      feature/move-jquery-from-cdn-to-local -> upstream/feature/move-jquery-from-cdn-to-local
 * [new branch]      gh-pages                              -> upstream/gh-pages

$ git merge upstream/gh-pages
Updating e244c7c..4b210e8
Fast-forward
 index.html | 1 +
 1 file changed, 1 insertion(+)

rebase

切换到开发分支 pr-2,然后 rebase 最新代码

$ git checkout pr-2
Switched to branch 'pr-2'
Your branch is up to date with 'origin/pr-2'.
$ git rebase -i gh-pages

执行 rebase,会进入如下编辑界面:

pick 700d844 add my pr part 2

# Rebase 4b210e8..700d844 onto 4b210e8 (1 command)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# ......

# 开头的都是注释,可以忽略。

这里什么都不做,直接输入:wq 保存退出,进行 rebase。

不出意外,这里会冲突,显示如下内容:

Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
error: could not apply 700d844... add my pr part 2
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 700d844... add my pr part 2

解决冲突

这时候就可以在本地解决冲突。

先来看看状态:

$ git status
interactive rebase in progress; onto 4b210e8
Last command done (1 command done):
   pick 700d844 add my pr part 2
No commands remaining.
You are currently rebasing branch 'pr-2' on '4b210e8'.
  (fix conflicts and then run "git rebase --continue")
  (use "git rebase --skip" to skip this patch)
  (use "git rebase --abort" to check out the original branch)

Unmerged paths:
  (use "git restore --staged <file>..." to unstage)
  (use "git add <file>..." to mark resolution)
	both modified:   index.html

no changes added to commit (use "git add" and/or "git commit -a")

$  git diff
diff --cc index.html
index 9547dfa,822a902..0000000
--- a/index.html
+++ b/index.html
@@@ -76,7 -76,7 +76,11 @@@
  <select class="select"></select>
  
  <section class="wrap">
++<<<<<<< HEAD
 +<p class="impression" style="color:blue">2022/9/14 By liubin part 1 </p>
++=======
+ <p class="impression" style="color:blue">2022/9/14 By liubin part 2 </p>
++>>>>>>> 700d844... add my pr part 2
  <p class="impression" style="color:blue">2022/9/13 From sysu </p>
  <p class="impression" style="color:red">2022/8/8 大家好,这是用于测试开展基于pull request的代码审查软件测试实验,非常感谢作者。 </p>
  <p class="impression"><mark>小陈同学来测试啦啦</mark></p>

解决办法也简单,就是用文本编辑器将需要的修改保留即可,也就是删除 ++ 开头的那三行:

++<<<<<<< HEAD
++=======
++>>>>>>> 700d844... add my pr part 2

编辑后再 diff 结果如下:

diff --cc index.html
index 9547dfa,822a902..0000000
--- a/index.html
+++ b/index.html
@@@ -76,7 -76,7 +76,8 @@@
  <select class="select"></select>
  
  <section class="wrap">
 +<p class="impression" style="color:blue">2022/9/14 By liubin part 1 </p>
+ <p class="impression" style="color:blue">2022/9/14 By liubin part 2 </p>
  <p class="impression" style="color:blue">2022/9/13 From sysu </p>

编辑完文件后,执行 git add .git rebase --continue 即可:

$ git add .
$ git rebase --continue

其中 git rebase --continue 会进入编辑页面,这也是编辑提交信息的界面,可以不用修改,直接 :wq 保存即可。

注意:在执行 git add 之前,请务必使用 git diff 查看修改内容是否正确。

推送新代码

这时候本地已经解决了冲突,可以推送新代码了。

$  git push --force

注意,这里必须加 --force 选项。

这时候到 GitHub 页面查看该 PR ,就可以看到如下状态了。

image

Clone this wiki locally