Rollback a Git merge

后端 未结 5 949
时光说笑
时光说笑 2020-12-04 05:02
develop branch
--> dashboard (working branch)

I use git merge --no-ff develop to merge any upstream changes into dashboard

相关标签:
5条回答
  • 2020-12-04 05:29

    Just reset the merge commit with git reset --hard HEAD^.

    If you use --no-ff git always creates a merge, even if you did not commit anything in between. Without --no-ff git will just do a fast forward, meaning your branches HEAD will be set to HEAD of the merged branch. To resolve this find the commit-id you want to revert to and git reset --hard $COMMITID.

    0 讨论(0)
  • 2020-12-04 05:31

    From here:

    http://www.christianengvall.se/undo-pushed-merge-git/

    git revert -m 1 <merge commit hash>
    

    Git revert adds a new commit that rolls back the specified commit.

    Using -m 1 tells it that this is a merge and we want to roll back to the parent commit on the master branch. You would use -m 2 to specify the develop branch.

    0 讨论(0)
  • 2020-12-04 05:33

    If you merged the branch, then reverted the merge using a pull request and merged that pull request to revert.

    The easiest way I felt was to:

    1. Take out a new branch from develop/master (where you merged)
    2. Revert the "revert" using git revert -m 1 xxxxxx (if the revert was merged using a branch) or using git revert xxxxxx if it was a simple revert
    3. The new branch should now have the changes you want to merge again.
    4. Make changes or merge this branch to develop/master
    0 讨论(0)
  • 2020-12-04 05:36

    Reverting a merge commit has been exhaustively covered in other questions. When you do a fast-forward merge, the second one you describe, you can use git reset to get back to the previous state:

    git reset --hard <commit_before_merge>
    

    You can find the <commit_before_merge> with git reflog, git log, or, if you're feeling the moxy (and haven't done anything else): git reset --hard HEAD@{1}

    0 讨论(0)
  • 2020-12-04 05:39
    git revert -m 1 88113a64a21bf8a51409ee2a1321442fd08db705
    

    But may have unexpected side-effects. See --mainline parent-number option in git-scm.com/docs/git-revert

    Perhaps a brute but effective way would be to check out the left parent of that commit, make a copy of all the files, checkout HEAD again, and replace all the contents with the old files. Then git will tell you what is being rolled back and you create your own revert commit :) !

    0 讨论(0)
提交回复
热议问题