Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undo a Git merge that hasn't been pushed yet

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?

like image 445
Matt Huggins Avatar asked Mar 05 '10 19:03

Matt Huggins


People also ask

How do I undo a merge not pushed?

You can undo a Git merge using the git reset –merge command. This command changes all files that are different between your current repository and a particular commit. There is no “git undo merge” command but the git reset command works well to undo a merge.

How do I revert a merge in git?

In case you are using the Tower Git client, undoing a merge is really simple: just press CMD+Z afterwards and Tower will undo the merge for you!

How do I undo attempted merge?

1 Answer. Git merge --abort # this will allow you to undo merge conflicts. This attempts to reset your working copy to whatever state it was in before the merge. That means that it should restore any uncommitted changes from before the merge, Generally, you shouldn't merge with uncommitted changes anyway.


2 Answers

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

like image 102
Marcin Gil Avatar answered Oct 15 '22 09:10

Marcin Gil


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.

like image 26
randomguy3 Avatar answered Oct 15 '22 07:10

randomguy3