Reverting a git merge while allowing for the same merge later)

后端 未结 3 1475
时光取名叫无心
时光取名叫无心 2020-12-07 00:14

What\'s the best way to revert a committed git merge while keeping the option of merging the same branch at a later point?

This is for a situation when I merge a bra

相关标签:
3条回答
  • 2020-12-07 00:26

    If you have not yet published your merge commit (pushed to the central server), the best solution is to rewrite history to remove the merge commit.

    1. If there are no other commits after the merge commit, reset the head to the previous commit:

    git reset HEAD^

    1. If there are other commits after the merge commit, you'll need to do a rebase and remove the merge commit, while re-applying the other commits.

    git rebase -i

    If you have published the merge, you will need to decide whether it is better to rewrite history to preserve proper merge potential for your branch, or simply add a revert commit. This will require talking to everyone who may have potentially downloaded the merge.

    If you cannot rewrite history, you'll need to do a revert:

    git revert -m 1

    In this case, a future merge of the branch will need manual intervention. You may need to cherry-pick commits that are not picked up by the merge, or revert your revert commit.

    0 讨论(0)
  • 2020-12-07 00:31

    If your merge commit is not pushed yet and it is the top one, you can use reset command

    git reset --hard HEAD^
    

    Note, that all uncommitted changes will be lost.

    0 讨论(0)
  • 2020-12-07 00:33

    Sorry to say that you have wasted your branch.

    But there is a workaround. The trick is to "rewrite" the merge commit temporarily so that it forgets that the branch is a parent. Assume X is the merge commit:

    git replace --graft X X^   # pretend that there is just one parent
    git merge branch           # merge the branch again
    git replace --delete X     # remove the replacement
    
    0 讨论(0)
提交回复
热议问题