Git 解决冲突的一次实例

背景

这里使用的是主干开发,分支发布的方式。我是在dev/6.12分支上开发,开发完成后需要合并到master,然后出现了冲突。

我希望我发布分支就是当前的开发分支,所以我是这样做的:先把主干代码合并到分支,解决冲突后再合并到master,然后使用分支上线。

原因是:master 是远程被保护的,不能本地提交,所以我在本地即使将分支合并到master,还是需要新开一个分支提交。

>>> 发现这样不可行之后,我使用master 新开一个分支,将dev/6.12合并过去,再提交到远程合并。

leedeMacBook-Pro:tdsp-startpage lee$ git branch
* dev/6.12
  master

使用rebase解决冲突

合并。可以参考:https://backlogtool.com/git-tutorial/cn/stepup/stepup2_8.html

leedeMacBook-Pro:tdsp-startpage lee$ git rebase master
First, rewinding head to replay your work on top of it...
Applying: add dyh card
Using index info to reconstruct a base tree...
M       tdsp-startpage-server/src/main/java/com/tudou/tdsp/startpage/service/bean/card/FeedBean.java
M       tdsp-startpage-server/src/main/java/com/tudou/tdsp/startpage/service/feedstream/FeedStreamService.java
M       tdsp-startpage-server/src/main/java/com/tudou/tdsp/startpage/service/feedstream/fetcher/IflowFetcher.java
M       tdsp-startpage-server/src/main/java/com/tudou/tdsp/startpage/service/feedstream/strategy/FeedSteamCombineStrategy.java
M       tdsp-startpage-server/src/main/java/com/tudou/tdsp/startpage/service/homepage/transfer/BaseTransService.java
Falling back to patching base and 3-way merge...
Auto-merging tdsp-startpage-server/src/main/java/com/tudou/tdsp/startpage/service/homepage/transfer/BaseTransService.java
Auto-merging tdsp-startpage-server/src/main/java/com/tudou/tdsp/startpage/service/feedstream/strategy/FeedSteamCombineStrategy.java
Auto-merging tdsp-startpage-server/src/main/java/com/tudou/tdsp/startpage/service/feedstream/fetcher/IflowFetcher.java
Auto-merging tdsp-startpage-server/src/main/java/com/tudou/tdsp/startpage/service/feedstream/FeedStreamService.java
CONFLICT (content): Merge conflict in tdsp-startpage-server/src/main/java/com/tudou/tdsp/startpage/service/feedstream/FeedStreamService.java
Auto-merging tdsp-startpage-server/src/main/java/com/tudou/tdsp/startpage/service/bean/card/FeedBean.java
error: Failed to merge in the changes.
Patch failed at 0001 add dyh card
The copy of the patch that failed is found in: .git/rebase-apply/patch

When you have resolved this problem, run "git rebase --continue".
If you prefer to skip this patch, run "git rebase --skip" instead.
To check out the original branch and stop rebasing, run "git rebase --abort".

其中有部分文件是分支修改了,然后合并成功。

部分文件和主干和分支共同修改了,但是合并成功了。

部分文件文件是merge失败了。需要解决的就是这部分合并。

解决冲突

leedeMacBook-Pro:tdsp-startpage lee$ git status
rebase in progress; onto 2c8ab4b
You are currently rebasing branch 'dev/6.12' on '2c8ab4b'.
  (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)

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   tdsp-startpage-server/src/main/java/com/tudou/tdsp/startpage/service/bean/card/FeedBean.java
        modified:   tdsp-startpage-server/src/main/java/com/tudou/tdsp/startpage/service/feedstream/fetcher/IflowFetcher.java
        modified:   tdsp-startpage-server/src/main/java/com/tudou/tdsp/startpage/service/feedstream/strategy/FeedSteamCombineStrategy.java
        modified:   tdsp-startpage-server/src/main/java/com/tudou/tdsp/startpage/service/homepage/transfer/BaseTransService.java
        new file:   tdsp-startpage-server/src/main/java/com/tudou/tdsp/startpage/service/homepage/transfer/DYHTransService.java
        modified:   tdsp-startpage-server/src/main/java/com/tudou/tdsp/startpage/service/homepage/transfer/PushTransService.java
        modified:   tdsp-startpage-server/src/main/java/com/tudou/tdsp/startpage/service/util/Constant.java

Unmerged paths:
  (use "git reset HEAD <file>..." to unstage)
  (use "git add <file>..." to mark resolution)

        both modified:   tdsp-startpage-server/src/main/java/com/tudou/tdsp/startpage/service/feedstream/FeedStreamService.java

需要解决FeedStreamService.java 文件冲突。

leedeMacBook-Pro:tdsp-startpage lee$ git add tdsp-startpage-server/src/main/java/com/tudou/tdsp/startpage/service/feedstream/FeedStreamService.java
leedeMacBook-Pro:tdsp-startpage lee$ git rebase --continue
Applying: add dyh card
leedeMacBook-Pro:tdsp-startpage lee$ git status
On branch dev/6.12
Your branch and 'origin/dev/6.12' have diverged,
and have 29 and 3 different commits each, respectively.
  (use "git pull" to merge the remote branch into yours)
nothing to commit, working tree clean

一般情况是这样,就可以解决了。

出现问题

git日志的异常

commit 734a3c977b9b5b1ee7f905974151e6a20ea9b5be (HEAD -> dev/6.12, origin/dev/6.12)
Merge: d516bcf b37cc2c
Author: 代替author <代替author邮箱>
Date:   Wed Sep 27 22:03:48 2017 +0800

    fix conflict

commit d516bcf34e9db39222b833007ec9b6b650cf24dc
Author: 代替author <代替author邮箱>
Date:   Wed Sep 27 20:38:38 2017 +0800

    add dyh card

commit b37cc2c3f0c14945767983304fec2e3a115556a8
Author: 代替author <代替author邮箱>
Date:   Wed Sep 27 20:38:38 2017 +0800

    add dyh card

为什么会出现相同的时间提交,但是内容和commit id不一样呢?

更改解决方法

leedeMacBook-Pro:tdsp-startpage lee$ git branch
  dev/6.12
* master
leedeMacBook-Pro:tdsp-startpage lee$ git checkout -b dev/y-6.12
Switched to a new branch 'dev/y-6.12'
leedeMacBook-Pro:tdsp-startpage lee$ git branch
  dev/6.12
* dev/y-6.12
  master

将dev/6.12的有用代码合并到这个新分支dev/y-6.12

leedeMacBook-Pro:tdsp-startpage lee$ git cherry-pick d516bcf34e9db39222b833007ec9b6b650cf24dc
error: could not apply d516bcf... add dyh card
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit'
leedeMacBook-Pro:tdsp-startpage lee$ git status
On branch dev/y-6.12
You are currently cherry-picking commit d516bcf.
  (fix conflicts and run "git cherry-pick --continue")
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

Changes to be committed:

        modified:   tdsp-startpage-server/src/main/java/com/tudou/tdsp/startpage/service/bean/card/FeedBean.java
        modified:   tdsp-startpage-server/src/main/java/com/tudou/tdsp/startpage/service/feedstream/fetcher/IflowFetcher.java
        modified:   tdsp-startpage-server/src/main/java/com/tudou/tdsp/startpage/service/feedstream/strategy/FeedSteamCombineStrategy.java
        modified:   tdsp-startpage-server/src/main/java/com/tudou/tdsp/startpage/service/homepage/transfer/BaseTransService.java
        new file:   tdsp-startpage-server/src/main/java/com/tudou/tdsp/startpage/service/homepage/transfer/DYHTransService.java
        modified:   tdsp-startpage-server/src/main/java/com/tudou/tdsp/startpage/service/homepage/transfer/PushTransService.java
        modified:   tdsp-startpage-server/src/main/java/com/tudou/tdsp/startpage/service/util/Constant.java

Unmerged paths:
  (use "git add <file>..." to mark resolution)

        both modified:   tdsp-startpage-server/src/main/java/com/tudou/tdsp/startpage/service/feedstream/FeedStreamService.java

解决冲突

leedeMacBook-Pro:tdsp-startpage lee$ git add tdsp-startpage-server/src/main/java/com/tudou/tdsp/startpage/service/feedstream/FeedStreamService.java
leedeMacBook-Pro:tdsp-startpage lee$ git status
On branch dev/y-6.12
You are currently cherry-picking commit d516bcf.
  (all conflicts fixed: run "git cherry-pick --continue")
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

Changes to be committed:

        modified:   tdsp-startpage-server/src/main/java/com/tudou/tdsp/startpage/service/bean/card/FeedBean.java
        modified:   tdsp-startpage-server/src/main/java/com/tudou/tdsp/startpage/service/feedstream/FeedStreamService.java
        modified:   tdsp-startpage-server/src/main/java/com/tudou/tdsp/startpage/service/feedstream/fetcher/IflowFetcher.java
        modified:   tdsp-startpage-server/src/main/java/com/tudou/tdsp/startpage/service/feedstream/strategy/FeedSteamCombineStrategy.java
        modified:   tdsp-startpage-server/src/main/java/com/tudou/tdsp/startpage/service/homepage/transfer/BaseTransService.java
        new file:   tdsp-startpage-server/src/main/java/com/tudou/tdsp/startpage/service/homepage/transfer/DYHTransService.java
        modified:   tdsp-startpage-server/src/main/java/com/tudou/tdsp/startpage/service/homepage/transfer/PushTransService.java
        modified:   tdsp-startpage-server/src/main/java/com/tudou/tdsp/startpage/service/util/Constant.java

leedeMacBook-Pro:tdsp-startpage lee$ git commit -m "fix conflick dev/6.12"
[dev/y-6.12 ab9851c] fix conflick dev/6.12
 Date: Wed Sep 27 20:38:38 2017 +0800
 8 files changed, 97 insertions(+), 5 deletions(-)
 create mode 100644 tdsp-startpage-server/src/main/java/com/tudou/tdsp/startpage/service/homepage/transfer/DYHTransService.java

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

开始在上面输入您的搜索词,然后按回车进行搜索。按ESC取消。

返回顶部