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

后端 未结 17 1641
无人及你
无人及你 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 07:42

    All the answers already covered most of the things but I will add my 5 cents. In short reverting a merge commit is quite simple:

    git revert -m 1 <commit-hash>
    

    If you have permission you can push it directly to the "master" branch otherwise simply push it to your "revert" branch and create pull request.

    You might find more useful info on this subject here: https://itcodehub.blogspot.com/2019/06/how-to-revert-merge-in-git.html

    0 讨论(0)
  • 2020-11-22 07:47

    I also faced this issue on a PR that has been merged to the master branch of a GitHub repo.

    Since I just wanted to modify some modified files but not the whole changes the PR brought, I had to amend the merge commit with git commit --am.

    Steps:

    1. Go to the branch which you want to change / revert some modified files
    2. Do the changes you want according to modified files
    3. run git add * or git add <file>
    4. run git commit --am and validate
    5. run git push -f

    Why it's interesting:

    • It keeps the PR's author commit unchanged
    • It doesn't break the git tree
    • You'll be marked as committer (merge commit author will remain unchanged)
    • Git act as if you resolved conflicts, it will remove / change the code in modified files as if you manually tell GitHub to not merge it as-is
    0 讨论(0)
  • 2020-11-22 07:51

    Here's a complete example in the hope that it helps someone:

    git revert -m 1 <commit-hash> 
    git push -u origin master
    

    Where <commit-hash> is the commit hash of the merge that you would like to revert, and as stated in the explanation of this answer, -m 1 indicates that you'd like to revert to the tree of the first parent prior to the merge.

    The git revert ... line essentially commits your changes while the second line makes your changes public by pushing them to the remote branch.

    0 讨论(0)
  • 2020-11-22 07:53

    The -m option specifies the parent number. This is because a merge commit has more than one parent, and Git does not know automatically which parent was the mainline, and which parent was the branch you want to un-merge.

    When you view a merge commit in the output of git log, you will see its parents listed on the line that begins with Merge:

    commit 8f937c683929b08379097828c8a04350b9b8e183
    Merge: 8989ee0 7c6b236
    Author: Ben James <ben@example.com>
    Date:   Wed Aug 17 22:49:41 2011 +0100
    
    Merge branch 'gh-pages'
    
    Conflicts:
        README
    

    In this situation, git revert 8f937c6 -m 1 will get you the tree as it was in 8989ee0, and git revert -m 2 will reinstate the tree as it was in 7c6b236.

    To better understand the parent IDs, you can run:

    git log 8989ee0 
    

    and

    git log 7c6b236
    
    0 讨论(0)
  • 2020-11-22 07:53

    I found creating a reverse patch between two know end-points and applying that patch would work. This presumes that you have created snapshots (tags) off of your master branch or even a back up of your master branch say master_bk_01012017.

    Say the code branch you merged into master was mycodebranch.

    1. Checkout master.
    2. Create a full binary reverse patch between master and your backup. git diff --binary master..master_bk_01012017 > ~/myrevert.patch
    3. Check your patch git apply --check myrevert.patch
    4. Apply patch with sign-off git am --signoff < myrevert.patch
    5. If you will need to bring in this code again once it is fixed, you will need to branch off the reverted master and checkout the fix branch git branch mycodebranch_fix git checkout mycodebranch_fix
    6. Here you need to find the SHA key for the revert and revert the revert git revert [SHA]
    7. Now you can use your mycodebranch_fix to fix the issues, commit and re-merge into master once done.
    0 讨论(0)
  • 2020-11-22 07:54

    The correctly marked answer worked for me but I had to spend some time to determine whats going on.. So I decided to add an answer with simple straightforward steps for cases like mine..

    Lets say we got branches A and B.. You merged branch A into branch B and pushed branch B to itself so now the merge is part of it.. But you want to go back to the last commit before the merge.. What do you do?

    1. Go to your git root folder (the project folder usually) and use git log
    2. You will see the history of recent commits - the commits have commit/author/date properties while the merges also have a merge property - so you see them like this:

      commit: <commitHash> Merge: <parentHashA> <parentHashB> Author: <author> Date: <date>

    3. Use git log <parentHashA> and git log <parentHashB> - you will see the commit histories of those parent branches - the first commits in the list are the latest ones

    4. Take the <commitHash> of the commit you want, go to your git root folder and use git checkout -b <newBranchName> <commitHash> - that will create a new branch starting from that last commit you've chosen before the merge.. Voila, ready!
    0 讨论(0)
提交回复
热议问题