Undo a Git merge that hasn't been pushed yet

前端 未结 30 2152
情歌与酒
情歌与酒 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 22:59

    With newer Git versions, if you have not committed the merge yet and you have a merge conflict, you can simply do:

    git merge --abort
    

    From man git merge:

    [This] can only be run after the merge has resulted in conflicts. git merge --abort will abort the merge process and try to reconstruct the pre-merge state.

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

    It is strange that the simplest command was missing. Most answers work, but undoing the merge you just did, this is the easy and safe way:

    git reset --merge ORIG_HEAD
    

    The ref ORIG_HEAD will point to the original commit from before the merge.

    (The --merge option has nothing to do with the merge. It's just like git reset --hard ORIG_HEAD, but safer since it doesn't touch uncommitted changes.)

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

    With git reflog check which commit is one prior the merge (git reflog will be a better option than git log). Then you can reset it using:

    git reset --hard commit_sha
    

    There's also another way:

    git reset --hard HEAD~1
    

    It will get you back 1 commit.

    Be aware that any modified and uncommitted/unstashed files will be reset to their unmodified state. To keep them either stash changes away or see --merge option below.


    As @Velmont suggested below in his answer, in this direct case using:

    git reset --hard ORIG_HEAD
    

    might yield better results, as it should preserve your changes. ORIG_HEAD will point to a commit directly before merge has occurred, so you don't have to hunt for it yourself.


    A further tip is to use the --merge switch instead of --hard since it doesn't reset files unnecessarily:

    git reset --merge ORIG_HEAD
    

    --merge

    Resets the index and updates the files in the working tree that are different between <commit> and HEAD, but keeps those which are different between the index and working tree (i.e. which have changes which have not been added).

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

    If you want a command-line solution, I suggest to just go with MBO's answer.

    If you're a newbie, you might like the graphical approach:

    1. Kick off gitk (from the command line, or right click in file browser if you have that)
    2. You can easily spot the merge commit there - the first node from the top with two parents
    3. Follow the link to the first/left parent (the one on your current branch before the merge, usually red for me)
    4. On the selected commit, right-click "Reset branch to here", pick the hard reset there
    0 讨论(0)
  • 2020-11-21 23:02

    If you didn't commit it yet, you can only use

    $ git checkout -f
    

    It will undo the merge (and everything that you did).

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

    The simplest answer is the one given by odinho - Velmont

    First do git reset --merge ORIG_HEAD

    For those looking to reset after changes are pushed, do this (Because this is the first post seen for any git reset merge questions)

    git push origin HEAD --force

    This will reset in a way that you won't get the merged changes back again after pull.

    0 讨论(0)
提交回复
热议问题