Undo a Git merge that hasn't been pushed yet

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

    I think you can do git rebase -i [hash] [branch_name] where [hash] is the identifying hash for however far back you want to rewind plus one (or however many commits back you want to go) and then delete the lines for the commits in the editor that you don't want any more. Save the file. Exit. Pray. And it should be rewound. You might have to do a git reset --hard, but it should be good at this point. You can also use this to pull specific commits out of a stack, if you don't want to keep them in your history, but that can leave your repository in a state that you probably don't want.

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

    The simplest of the simplest chance, much simpler than anything said here:

    Remove your local branch (local, not remote) and pull it again. This way you'll undo the changes on your master branch and anyone will be affected by the change you don't want to push. Start it over.

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

    Strategy: Create a new branch from where everything was good.

    Rationale: Reverting a merge is hard. There are too many solutions, depending on many factors such as whether you've committed or pushed your merge or if there were new commits since your merge. Also you still need to have a relatively deep understanding of git to adapt these solutions to your case. If you blindly follow some instructions, you can end up with an "empty merge" where nothing will be merged, and further merge attempts will make Git tell you "Already up to date".

    Solution:

    Let's say you want to merge dev into feature-1.

    1. Find the revision that you want to receive the merge:

      git log --oneline feature-1
      a1b2c3d4 Merge branch 'dev' into 'feature-1' <-- the merge you want to undo
      e5f6g7h8 Fix NPE in the Zero Point Module <-- the one before the merge, you probably want this one
      
    2. Check it out (go back in time):

      git checkout e5f6g7h8
      
    3. Create a new branch from there and check it out:

      git checkout -b feature-1
      

    Now you can restart your merge:

    1. Merge: git merge dev

    2. Fix your merge conflicts.

    3. Commit: git commit

    4. When you're satisfied with the results, delete the old branch: git branch --delete feature-1

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-11-21 23:09

    You have to change your HEAD, Not yours of course but git HEAD....

    So before answering let's add some background, explaining what is this HEAD.

    First of all what is HEAD?

    HEAD is simply a reference to the current commit (latest) on the current branch.
    There can only be a single HEAD at any given time. (excluding git worktree)

    The content of HEAD is stored inside .git/HEAD and it contains the 40 bytes SHA-1 of the current commit.


    detached HEAD

    If you are not on the latest commit - meaning that HEAD is pointing to a prior commit in history its called detached HEAD.

    On the command line, it will look like this- SHA-1 instead of the branch name since the HEAD is not pointing to the tip of the current branch

    A few options on how to recover from a detached HEAD:


    git checkout

    git checkout <commit_id>
    git checkout -b <new branch> <commit_id>
    git checkout HEAD~X // x is the number of commits t go back
    

    This will checkout new branch pointing to the desired commit.
    This command will checkout to a given commit.
    At this point, you can create a branch and start to work from this point on.

    # Checkout a given commit. 
    # Doing so will result in a `detached HEAD` which mean that the `HEAD`
    # is not pointing to the latest so you will need to checkout branch
    # in order to be able to update the code.
    git checkout <commit-id>
    
    # create a new branch forked to the given commit
    git checkout -b <branch name>
    

    git reflog

    You can always use the reflog as well.
    git reflog will display any change which updated the HEAD and checking out the desired reflog entry will set the HEAD back to this commit.

    Every time the HEAD is modified there will be a new entry in the reflog

    git reflog
    git checkout HEAD@{...}
    

    This will get you back to your desired commit


    git reset --hard <commit_id>

    "Move" your HEAD back to the desired commit.

    # This will destroy any local modifications.
    # Don't do it if you have uncommitted work you want to keep.
    git reset --hard 0d1d7fc32
    
    # Alternatively, if there's work to keep:
    git stash
    git reset --hard 0d1d7fc32
    git stash pop
    # This saves the modifications, then reapplies that patch after resetting.
    # You could get merge conflicts if you've modified things which were
    # changed since the commit you reset to.
    
    • Note: (Since Git 2.7)
      you can also use the git rebase --no-autostash as well.

    git revert <sha-1>

    "Undo" the given commit or commit range.
    The reset command will "undo" any changes made in the given commit.
    A new commit with the undo patch will be committed while the original commit will remain in the history as well.

    # add new commit with the undo of the original one.
    # the <sha-1> can be any commit(s) or commit range
    git revert <sha-1>
    

    This schema illustrates which command does what.
    As you can see there reset && checkout modify the HEAD.

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

    You should reset to the previous commit. This should work:

    git reset --hard HEAD^
    

    Or even HEAD^^ to revert that revert commit. You can always give a full SHA reference if you're not sure how many steps back you should take.

    In case when you have problems and your master branch didn't have any local changes, you can reset to origin/master.

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