Undo a Git merge that hasn't been pushed yet

前端 未结 30 2249
情歌与酒
情歌与酒 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:06

    Okay, the answers other people here gave me were close, but it didn't work. Here's what I did.

    Doing this...

    git reset --hard HEAD^
    git status
    

    ...gave me the following status.

    # On branch master
    # Your branch and 'origin/master' have diverged,
    # and have 3 and 3 different commit(s) each, respectively.
    

    I then had to type in the same git reset command several more times. Each time I did that, the message changed by one as you can see below.

    > git reset --hard HEAD^
    HEAD is now at [...truncated...]
    > git status
    # On branch master
    # Your branch and 'origin/master' have diverged,
    # and have 3 and 3 different commit(s) each, respectively.
    > git reset --hard HEAD^
    HEAD is now at [...truncated...]
    > git status
    # On branch master
    # Your branch and 'origin/master' have diverged,
    # and have 2 and 3 different commit(s) each, respectively.
    > git reset --hard HEAD^
    HEAD is now at [...truncated...]
    > git status
    # On branch master
    # Your branch and 'origin/master' have diverged,
    # and have 1 and 3 different commit(s) each, respectively.
    > git reset --hard HEAD^
    HEAD is now at [...truncated...]
    > git status
    # On branch master
    # Your branch is behind 'origin/master' by 3 commits, and can be fast-forwarded.
    

    At this point, I saw the status message changed, so I tried doing a git pull, and that seemed to work:

    > git pull
    Updating 2df6af4..12bbd2f
    Fast forward
     app/views/truncated |    9 ++++++---
     app/views/truncated |   13 +++++++++++++
     app/views/truncated |    2 +-
     3 files changed, 20 insertions(+), 4 deletions(-)
    > git status
    # On branch master
    

    So long story short, my commands came down to this:

    git reset --hard HEAD^
    git reset --hard HEAD^
    git reset --hard HEAD^
    git reset --hard HEAD^
    git pull
    

提交回复
热议问题