Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: move HEAD back to a previous commit

Tags:

git

I've made some changes in my master branch that I no longer see fit. For arguments sake, I have a commit hash named 791fda4e1ac0e1a393e01340bf0fba3f333a73ff that I'd like to make my HEAD now, as that's when everything was stable in the repo. I've tried to do the following:

git reset 791fda4e1ac 
git reset --soft HEAD@{1} 
git commit -m "Revert to 791fda4e1ac"
git reset --hard 

Yet, when I do a git push origin, I get rejected, because origin thinks it's a non-fastforward push:

 ! [rejected]        master -> master (non-fast-forward)

What's the right way of reverting my HEAD back to commit hash 791fda4e1ac and getting the origin server there as well?

like image 233
randombits Avatar asked Jan 09 '13 17:01

randombits


2 Answers

It's rejected because it is non-fast-forward — it discards history others may have built on.

Use git revert instead to create a new commit which undoes the effect of the existing ones.

Or, if you're sure no one else is using your repository and you don't care about those commits in the future, go ahead and git push -f to ignore the warning.

like image 104
Kevin Reid Avatar answered Sep 25 '22 19:09

Kevin Reid


You have to do a forced push (git push -f origin).

like image 38
dty Avatar answered Sep 25 '22 19:09

dty