Git rollback 1 pull

Bastian picture Bastian · Apr 17, 2012 · Viewed 88.4k times · Source

I have a web server that serves a project that is a git repository. When I do some changes to the code I then do a git pull from the server. Sometimes the new code just crashes, I would like to be able to do a rollback to the latest pull, the one just before. I want to do that with a script, without having to search what is the latest sha. How can I do that?

Edit: Just to clarify, I just want to have to do one action, like pushing a button that says "oops! this latest pull I just did was a mistake, I wish I didn't do that". I don't want to have to look for sha or tags or anything else in this situation, it's more like an 'undo' function. Then I want to be able to continue working on the code and the next pull on the server needs to bring the latest changes.

Answer

Karl Bielefeldt picture Karl Bielefeldt · Apr 17, 2012

git reset --hard HEAD^1 will take you back one commit from what you pulled. If you want it back to the state it was in before you pulled, use git reset --hard HEAD@{1}. The @{1} tracks where the head was at before the last operation that changed it in your local repo, so it will go back several commits if several were pushed before you did your pull. Also see git reflog to show the entire list.