I don't know about you, but this happens to me all the time; I accidentally take a pull in the wrong branch, then curse a bit, call myself 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 keeps a copy of your remote at ORIG_HEAD
so if you need to revert to it, you can tell Git to reset back to the remote version by using the following command:
git reset --hard ORIG_HEAD
This solution works great in most cases, but sometimes you only want to move back a few commits, or the merge/pull has not gone through 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 the 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
This solution can also be used to:Originally Posted On: StackOverflow
- Resetting a git branch back to it's remote HEAD
- Getting rid of git merge conflicts
- Resetting HEAD to last stable commit in git