Resetting a git pull from the wrong branch

Written on by Mandeep Singh in Accidents Recovery

I don't know about you, but this happens with me all the time, I accidentally take a pull in the wrong branch, then curse a bit, call my self some name 🤬, and then apply this fix. This fix has been a lifesaver for me, and it's also effortless if you understand the concept well enough.

Let's say you pulled in a develop branch into your master and the code is not yet ready to go live, now the good thing about git is that it always keep a copy of your remote at ORIG_HEAD so if you need to revert to it, you can tell git to reset back to remote version by using the following command:

git reset --hard ORIG_HEAD

Now, this solution works great in most cases, but some times you only want to move back a few commits, or the merge/pull has not gone through all the way, due to conflicts. In those cases, you can tell git only to discard current changes i.e.

git reset --hard

or in case of resetting last three commits:

git reset --hard HEAD~2

I hope this will save you much headache every month as it does for me 😉

PS: you can check your HEAD counts on how far you want to go using git reflog

Other uses of this solution

  • Resetting a git branch back to it's remote HEAD
  • Getting rid of git merge conflicts
  • Resetting HEAD to last stable commit in git

Sign up below to receive updates, tips and more handy tutorials. It’s good stuff & I don’t spam.