How do you revert a faulty git merge commit

后端 未结 2 384
走了就别回头了
走了就别回头了 2021-01-01 20:11

So one of my coworkers accidentally made a merge that actually only kept one side of the tree. So he started a merge, deleted all the changes introduced by the merge, and th

相关标签:
2条回答
  • 2021-01-01 20:59

    The best rule of thumb here is to never ever change the history of your repo after it has been made public. It really screws things up. I don't think this case can avoid it though.

    I think there are a couple of options here but the simplest is to do a rebase. Using your repo, these are the commands I used:

    git rebase -i ead3646
    

    Then, when the interactive shell comes up, remove the entire line of the faulty commit. Save that and you should wind up with a new history like this:

    * f0ab6d5 more normal work on dev
    * ebb5103 idx commit
    * ead3646 master change
    * 582c38c dev commits
    * f4b8bc6 initial commit
    

    Getting you two (and others) back in sync is going to take some branching, pushing, emailing and lunch buying.

    0 讨论(0)
  • 2021-01-01 21:03

    The git documentation says this about reverting merges: git/Documentation/howto/revert-a-faulty-merge.txt although this is mostly about reverting merges that brought in bad changes, rather than reverting merges that were done incorrectly.

    Basically, reverting a merge will undo the data changes, but not the history (graph) changes. Therefore it is expected that reverting your faulty merge does nothing.

    One way you can deal with this is to do the merge again, then merge the result of that into master. In your example this could be like:

    git checkout -b temp/merge-fixup ead364653b601f48159bca5cb59d6a204a426168
    git merge 2fce9bfe8f721c45ea1ed5f93176322cac60a1d9
    git checkout master
    git merge temp/merge-fixup
    git branch -d temp/merge-fixup
    
    0 讨论(0)
提交回复
热议问题