Revert a merge after being pushed

前端 未结 4 1743
灰色年华
灰色年华 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: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.

提交回复
热议问题