Undo a Git merge that hasn't been pushed yet

前端 未结 30 2156
情歌与酒
情歌与酒 2020-11-21 22:27

Within my master branch, I did a git merge some-other-branch locally, but never pushed the changes to origin master. I didn\'t mean to merge, so I\'d like to un

相关标签:
30条回答
  • 2020-11-21 23:13

    You could use git reflog to find the previous checkout. Sometimes that's a good state you want to return back to.

    Concretely,

    $ git reflog
    $ git reset --hard HEAD@{0}
    
    0 讨论(0)
  • 2020-11-21 23:13

    I was able to resolve this problem with a single command that doesn't involve looking up a commit id.

    git reset --hard remotes/origin/HEAD
    

    The accepted answer didn't work for me but this command achieved the results I was looking for.

    0 讨论(0)
  • 2020-11-21 23:13

    Just for an extra option to look at, I've been mostly following the branching model described here: http://nvie.com/posts/a-successful-git-branching-model/ and as such have been merging with --no-ff (no fast forward) usually.

    I just read this page as I'd accidentally merged a testing branch instead of my release branch with master for deploying (website, master is what is live). The testing branch has two other branches merged to it and totals about six commits.

    So to revert the whole commit I just needed one git reset --hard HEAD^ and it reverted the whole merge. Since the merges weren't fast forwarded the merge was a block and one step back is "branch not merged".

    0 讨论(0)
  • 2020-11-21 23:14

    If you are in a middle of merging you can always abort it git merge --abort

    0 讨论(0)
  • 2020-11-21 23:14

    You can use only two commands to revert a merge or restart by a specific commit:

    1. git reset --hard commitHash (you should use the commit that you want to restart, eg. 44a587491e32eafa1638aca7738)
    2. git push origin HEAD --force (Sending the new local master branch to origin/master)

    Good luck and go ahead!

    0 讨论(0)
  • 2020-11-21 23:15

    If you committed the merge:

    git reset HEAD~1
    # Make sure what you are reverting is in fact the merge files
    git add .
    git reset --hard
    
    0 讨论(0)
提交回复
热议问题