Revert a merge after being pushed

前端 未结 4 1641
灰色年华
灰色年华 2021-01-30 05:19

Steps i performed:

I have two branches branch1 and branch2,

$git branch --Initial state
$branch1

$git checkout branch2
$git pull origin branch1 --Step1
         


        
相关标签:
4条回答
  • 2021-01-30 05:22

    Try using git reflog <branch> to find out where your branch was before the merge and git reset --hard <commit number> to restore the old revision.

    Reflog will show you older states of the branch, so you can return it to any change set you like.

    Make sure you are in correct branch when you use git reset

    To change remote repository history, you can do git push -f, however this is not recommended because someone can alredy have downloaded changes, pushed by you.

    0 讨论(0)
  • 2021-01-30 05:23

    The first option is the use of git revert.

    git revert -m 1 [sha-commit-before-merge]
    

    The git revert will revert the changes but will keep the history. Therefore you will not be able to continue working in the same branch since you cannot see the actual difference between the merged branch and your feature branch anymore. Use the following way to remove history as well. Do this very carefully if and only if you are the only one pushing changes to the branch at the moment.

    git reset --hard [sha-commit-before-merge]
    git push [origin] [branch] --force
    
    0 讨论(0)
  • 2021-01-30 05:42

    In my case, I merged my branch (say: my-branch) with another feature branch (feature-branch) but not master. So my branch history was like this:

    my-branch (before merge)
    
    ---master----m1----m2----m3---m4
    

    After merging it with another feature-branch which had commits f1, f2 on top of master, it became like this:

    my-branch (after merge)
    
    ---master----m1----m2----f1----f2----m3---m4----mergecommit
    

    This might have happened because while working on my branch I did a merge from master after 2 commits, or one of 2 branches might not have been up to date with master. So in this case git revert -m 1 was not working as it was leaving those f1 and f2 commits in between.

    The solution was simple, which will work in case of normal scenarios, where we don't have in-between commits:

    git rebase -i HEAD~6
    

    Instead of 6 use appropriate number based on how many past commits you want to change. Now Vim editor is opened, just mark undesired commits as drop and same and quit using :wq verify log:

    git log --oneline 
    

    force push

    git push -f
    

    Now the remote branch should be in previous state.

    0 讨论(0)
  • 2021-01-30 05:46

    You can revert the merge following the official guide, however this leaves Git with the erroneous belief that the merged commits are still on the target branch.

    Basically you have to :

    git revert -m 1 (Commit id of the merge commit)
    
    0 讨论(0)
提交回复
热议问题