Git: Wrong Merge, then reverted the merge. Now not able to merge the branch again

前端 未结 2 398
轮回少年
轮回少年 2021-01-07 07:47

I\'m in a little trouble with git.

Here is what i did.

  1. I merged latest master to my branch and pushed it
  2. Later realised that this merge is cor
2条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-07 08:04

    Let's say that your feature branch feature and master started off like this:

    master:  A -> B -> C
    feature: A -> D
    

    After merging master into feature, this is how things looked:

    master:  A -> B -> C
    feature: A -> D -> M        # M is a merge commit
    

    Next your reverted the merge in the feature branch by doing a git revert. This means that you told Git to add a new commit to undo the result of the merge. Now this is the state of the two branches:

    master:  A -> B -> C
    feature: A -> D -> M -> R   # R is a revert commit
    

    When you try to pull master into feature, Git is telling you that you are already up to date, because you are! The revert commit functionally undid whatever happened during the merge, but now you have two new commits in your feature branch.

    To get back to the state you were in before you made the erroneous merge in your feature branch, you can nuke the two merge and revert commits in feature. Follow these steps exactly:

    git checkout feature        # switch to your feature branch
    git reset --hard HEAD~2     # nuke the 'M' and 'R' commits
    

    After this, you can try doing a merge with master again and all should be fine. Of course, make sure that you do the merge properly.

    Note that this option involves rewriting the history of the feature branch, via nuking commits, and should probably not be used if the branch is shared and you have already pushed the branch with the merge commit. See the answer by @torek for other options if you fall into this category.

提交回复
热议问题