Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to undo a merge in GitHub

Tags:

git

github

revert

I have been working in PhpStorm on a dedicated branch, but when pushing to github, I inadvertently merged to the master branch.

How would I undo the merge both in github and locally? The github master is used to migrate code to various servers so I need to rollback to the previous commit prior to the merge rather than create a new commit with my changes undone.

like image 313
Typhoon101 Avatar asked Mar 17 '17 14:03

Typhoon101


People also ask

How do I Unmerge a merge commit?

You can use the Git reset command to undo a merge. Firstly, you need to check for the commit hash (or id) so you can use it to go back to the previous commit. To check for the hash, run git log or git reflog . git reflog is a better option because things are more readable with it.


2 Answers

You need to reset the head to the commit just before your current head.

git reset --hard <commit_before_merge>

E.g. git reset --hard master^

like image 114
kkflf Avatar answered Oct 05 '22 12:10

kkflf


To answer this more succinctly:

git checkout master
git reset --hard <commit_before_merge>
git push -f
like image 7
John Doherty Avatar answered Oct 05 '22 13:10

John Doherty