Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git rollback 1 pull

Tags:

git

rollback

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.

like image 909
Bastian Avatar asked Apr 17 '12 18:04

Bastian


People also ask

Can we rollback git pull?

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.

Can I pull one file from git?

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).


2 Answers

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.

like image 148
Karl Bielefeldt Avatar answered Oct 21 '22 01:10

Karl Bielefeldt


There is another way to discard last pull

git reset --keep HEAD@{1} 
like image 20
Riajul Islam Avatar answered Oct 21 '22 02:10

Riajul Islam