How to revert a merge commit that's already pushed to remote branch?

后端 未结 17 1645
无人及你
无人及你 2020-11-22 07:04

git revert alone won\'t work. -m must be specified, and I\'m pretty confused about it.

Anyone experienced this before?<

17条回答
  •  终归单人心
    2020-11-22 08:05

    If you want to revert a merge commit, here is what you have to do.

    1. First, check the git log to find your merge commit's id. You'll also find multiple parent ids associated with the merge (see image below).

    Note down the merge commit id shown in yellow. The parent IDs are the ones written in the next line as Merge: parent1 parent2. Now...

    Short Story:

    1. Switch to branch on which the merge was made. Then Just do the git revert -m 1 which will open a vi console for entering commit message. Write, save, exit, done!

    Long story:

    1. Switch to branch on which the merge was made. In my case, it is the test branch and I'm trying to remove the feature/analytics-v3 branch from it.

    2. git revert is the command which reverts any commit. But there is a nasty trick when reverting a merge commit. You need to enter the -m flag otherwise it will fail. From here on, you need to decide whether you want to revert your branch and make it look like exactly it was on parent1 or parent2 via:

    git revert -m 1 (reverts to parent2)

    git revert -m 2 (reverts to parent1)

    You can git log these parents to figure out which way you want to go and that's the root of all the confusion.

提交回复
热议问题