26

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.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Typhoon101
  • 2,063
  • 8
  • 32
  • 49

2 Answers2

33

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^

kkflf
  • 2,435
  • 3
  • 26
  • 42
  • 3
    That will reset my local branch. Is it then just a case of pushing to update github? – Typhoon101 Mar 17 '17 at 14:51
  • You can either reset or checkout the previous commit and push that to github as head. Checkout the previous commit `checkout master^` – kkflf Mar 18 '17 at 18:42
  • But hard reset or previous commit will need force push, right? A force push overwrites public history and requires everyone who has cloned the repo to rebase their clone. – Siddu Oct 09 '19 at 15:50
  • 5
    @Siddu Sorry for getting back to you so late. Yes, you have to do a force push. `git push -f`. – kkflf Dec 16 '19 at 20:48
9

To answer this more succinctly:

git checkout master
git reset --hard <commit_before_merge>
git push -f
John Doherty
  • 3,669
  • 36
  • 38