Undo a Git merge that hasn't been pushed yet

GitUndoGit Merge

Git Problem Overview

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 undo it. When doing a git status after my merge, I was getting this message:

# On branch master
# Your branch is ahead of 'origin/master' by 4 commits.

Based upon some instructions I found, I tried running

git revert HEAD -m 1

but now I'm getting this message with git status:

# On branch master
# Your branch is ahead of 'origin/master' by 5 commits.

I don't want my branch to be ahead by any number of commits. How do I get back to that point?

Git Solutions

Solution 1 - Git

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).

Solution 2 - Git

Assuming your local master was not ahead of origin/master, you should be able to do

git reset --hard origin/master

Then your local master branch should look identical to origin/master.

Solution 3 - Git

See chapter 4 in the Git book and the original post by Linus Torvalds.

To undo a merge that was already pushed:

git revert -m 1 commit_hash

Be sure to revert the revert if you're committing the branch again, like Linus said.

Solution 4 - Git

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.)

Solution 5 - Git

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.

Solution 6 - Git

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.

Solution 7 - Git

Lately, I've been using git reflog to help with this. This mostly only works if the merge JUST happened, and it was on your machine.

git reflog might return something like:

fbb0c0f HEAD@{0}: commit (merge): Merge branch 'master' into my-branch
43b6032 HEAD@{1}: checkout: moving from master to my-branch
e3753a7 HEAD@{2}: rebase finished: returning to refs/heads/master
e3753a7 HEAD@{3}: pull --rebase: checkout e3753a71d92b032034dcb299d2df2edc09b5830e
b41ea52 HEAD@{4}: reset: moving to HEAD^
8400a0f HEAD@{5}: rebase: aborting

The first line indicates that a merge occurred. The 2nd line is the time before my merge. I simply git reset --hard 43b6032 to force this branch to track from before the merge, and carry-on.

Solution 8 - Git

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

git merge --abort

Solution 9 - Git

With modern Git, you can:

git merge --abort

Older syntax:

git reset --merge


git reset --hard

But actually, it is worth noticing that git merge --abort is only equivalent to git reset --merge given that MERGE_HEAD is present. This can be read in the Git help for merge command.

git merge --abort is equivalent to git reset --merge when MERGE_HEAD is present.

After a failed merge, when there is no MERGE_HEAD, the failed merge can be undone with git reset --merge, but not necessarily with git merge --abort, so they are not only old and new syntax for the same thing.

Personally I find git reset --merge much more powerful and useful in everyday work, so that's the one I always use.

Solution 10 - Git

If branches are merged and not pushed, then git reset command given below will work to undo the merge:

git reset --merge ORIG_HEAD


git reset --merge origin/master

Solution 11 - Git

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

Solution 12 - Git

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.

enter image description here

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

enter image description here enter image description here

###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

enter image description here

###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.

enter image description here

Solution 13 - Git

It can be done multiple ways.

1) Abort Merge

If you are in-between a bad merge (mistakenly done with wrong branch), and wanted to avoid the merge to go back to the branch latest as below:

git merge --abort

2) Reset HEAD to remote branch

If you are working from remote develop branch, you can reset HEAD to the last commit on remote branch as below:

git reset --hard origin/develop

3) Delete current branch, and checkout again from the remote repository

Considering, you are working on develop branch in local repo, that syncs with remote/develop branch, you can do as below:

git checkout master 
##to delete one branch, you need to be on another branch, otherwise you will fall with the branch :) 

git branch -D develop
git checkout -b develop origin/develop

Solution 14 - Git

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


$ git reflog
$ git reset --hard HEAD@{0}

Solution 15 - Git

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.

Solution 16 - Git

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

$ git checkout -f

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

Solution 17 - Git

Got to this question also looking to revert to match origin (ie, NO commits ahead of origin). Researching further, found there's a reset command for exactly that:

git reset --hard @{u}

Note: @{u} is shorthand for origin/master. (And, of course, you need that remote repository for this to work.)

Solution 18 - Git

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.

Solution 19 - Git

Answering the question "Undo a Git merge that hasn't been pushed yet"

>You can use git reset --hard HEAD~1

Consider the following situation where there are 2 branches master and feature-1:

$ git log --graph --oneline --all

> Do Git merge

$ git merge feature-1

$ git log --graph --oneline --all

> Undo Git merge

$ git reset --hard HEAD~1

$ git log --graph --oneline --all

Solution 20 - Git

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!

Solution 21 - Git

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".

Solution 22 - Git

If your merge and the corresponding commits were not pushed yet, you can always switch to another branch, delete the original one and re-create it.

For example, I accidentally merged a develop branch into master and wanted to undo that. Using the following steps:

git checkout develop
git branch -D master
git branch -t master origin/master

Voila! Master is at the same stage as origin, and your mis-merged state is erased.

Solution 23 - Git

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

Solution 24 - Git

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".


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

Solution 25 - Git

Just create new branch, then cherry-pick desired commits to it.

Its saver and simpler then resets described in many answers above

Solution 26 - Git

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.

Solution 27 - Git

  1. First, make sure that you've committed everything.

  2. Then reset your repository to the previous working state:

     $ git reset f836e4c1fa51524658b9f026eb5efa24afaf3a36

or using --hard (this will remove all local, not committed changes!):

    $ git reset f836e4c1fa51524658b9f026eb5efa24afaf3a36 --hard

Use the hash which was there before your wrongly merged commit.

3. Check which commits you'd like to re-commit on the top of the previous correct version by:

    $ git log 4c3e23f529b581c3cbe95350e84e66e3cb05704f

    commit 4c3e23f529b581c3cbe95350e84e66e3cb05704f


    commit 16b373a96b0a353f7454b141f7aa6f548c979d0a


4. Apply your right commits on the top of the right version of your repository by:

* By using cherry-pick (the changes introduced by some existing commits)

            git cherry-pick ec59ab844cf504e462f011c8cc7e5667ebb2e9c7

* Or by cherry-picking the range of commits by:
    * First checking the right changes before merging them:

            git diff 5216b24822ea1c48069f648449997879bb49c070..4c3e23f529b581c3cbe95350e84e66e3cb05704f

    * First checking the right changes before merging them:

            git cherry-pick 5216b24822ea1c48069f648449997879bb49c070..4c3e23f529b581c3cbe95350e84e66e3cb05704f

      where this is the range of the correct commits which you've committed (excluding wrongly committed merge).

Solution 28 - Git

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.

Solution 29 - Git

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

Solution 30 - Git

  1. git stash

  2. git branch -d the_local_branch

  3. git checkout -t <name of remote>

  4. git stash apply

This worked for me..!!

Solution 31 - Git

If you notice that you need to revert immediately after the merge and you haven't done anything else after the merge attempt, you can just issue this command: git reset --hard HEAD@{1}.

Essentially, your merge sha will be pointing to HEAD@{0} if nothing else was committed after the merge and so HEAD@{1} will be the previous point before the merge.

Solution 32 - Git

In this case, you will want to reset your branch with git reset --hard <branch_name>. If you want to save your changes before reseting them be sure to create a new branch and git checkout <branch_name>.

You can reset the state to a specific commit with git reset --hard <commit_id> as well.

If the changes have been pushed you can use git revert <branch_name> instead. Be sure to check out how to use git revert and git checkout in other scenarios as well.

Solution 33 - Git

You can use the git-reset command.

> git-reset - Reset current HEAD to the > > specified state. git reset [--mixed | > > --soft | --hard | --merge] [-q] [] git reset [-q] [] > > [--] … git reset --patch > > [] [--] […]



All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMatt HugginsView Question on Stackoverflow
Solution 1 - GitMarcin GilView Answer on Stackoverflow
Solution 2 - Gitrandomguy3View Answer on Stackoverflow
Solution 3 - GitYuri GeinishView Answer on Stackoverflow
Solution 4 - Gitodinho - VelmontView Answer on Stackoverflow
Solution 5 - GitTravis ReederView Answer on Stackoverflow
Solution 6 - GitMBOView Answer on Stackoverflow
Solution 7 - GitParrisView Answer on Stackoverflow
Solution 8 - GitllioorView Answer on Stackoverflow
Solution 9 - GitMartin GView Answer on Stackoverflow
Solution 10 - Gitbansi923View Answer on Stackoverflow
Solution 11 - GitMatt HugginsView Answer on Stackoverflow
Solution 12 - GitCodeWizardView Answer on Stackoverflow
Solution 13 - GitAmit KaneriaView Answer on Stackoverflow
Solution 14 - GitstephjangView Answer on Stackoverflow
Solution 15 - GitRalph RitochView Answer on Stackoverflow
Solution 16 - GitIdealmindView Answer on Stackoverflow
Solution 17 - GitleanneView Answer on Stackoverflow
Solution 18 - GitHarshaView Answer on Stackoverflow
Solution 19 - GitblessedView Answer on Stackoverflow
Solution 20 - GitMatheus AbreuView Answer on Stackoverflow
Solution 21 - GitDamien ByrneView Answer on Stackoverflow
Solution 22 - GitStephanView Answer on Stackoverflow
Solution 23 - GitingerView Answer on Stackoverflow
Solution 24 - GitpybView Answer on Stackoverflow
Solution 25 - GitgdbdableView Answer on Stackoverflow
Solution 26 - GittychoishView Answer on Stackoverflow
Solution 27 - GitkenorbView Answer on Stackoverflow
Solution 28 - GitLuisView Answer on Stackoverflow
Solution 29 - GitDorianView Answer on Stackoverflow
Solution 30 - GitSiva KumarView Answer on Stackoverflow
Solution 31 - GitDut A.View Answer on Stackoverflow
Solution 32 - GitNesha ZoricView Answer on Stackoverflow
Solution 33 - GitNebuSoftView Answer on Stackoverflow