Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undo a git merge (hasn't been pushed yet)

Tags:

git

merge

revert

I just committed some changes into one of my feature branches ("feedback_tab") then, checked out "master" and merged them in there. I actually meant to merge them into my "development" branch.

Now, master is ahead of 'origin/master' (its remote) by 17 commits - I haven't pushed the merge up (and don't want to, obviously). How can I revert master back to the same state as before the accidental merge? I'm confused between git revert and git reset with this stuff.

I looked in my git log and there's no entry for merging feedback_tab into master. I'd have thought it would be the top entry?

Bit confused :/ any help welcome! max

like image 444
Max Williams Avatar asked May 10 '11 14:05

Max Williams


People also ask

How do I undo a git merge?

You can use the git reset --merge command. You can also use the git merge --abort command.

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

To undo a merge that was NOT pushed:

git reset --merge ORIG_HEAD 

If during the merge you get a conflict, the best way to undo the merge is:

git merge --abort 
like image 155
Hemerson Varela Avatar answered Sep 24 '22 16:09

Hemerson Varela


git reset --hard HEAD~17 takes you back 17 commits ahead of the head of master. git rebase -i HEAD~17 probably gets rid of the extra commits as well.

like image 20
Tamás Avatar answered Sep 24 '22 16:09

Tamás