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.
There is no command to explicitly undo the git pull command. The alternative is to use git reset, which reverts a repository back to a previous commit.
Short Answergit checkout origin/master -- path/to/file // git checkout <local repo name (default is origin)>/<branch name> -- path/to/file will checkout the particular file from the downloaded changes (origin/master).
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.
There is another way to discard last pull
git reset --keep HEAD@{1}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With