Git rollback 1 pull
Asked Answered
L

9

65

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.

Lancer answered 17/4, 2012 at 18:12 Comment(0)
U
164

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.

Uncounted answered 17/4, 2012 at 19:31 Comment(2)
very interesting, and then how would I return to normal operation? I mean if do some corrections on the bad code, can I just go to the server and do a git pull again to get to the latest changes?Lancer
Yes, it will be in a state like you never pulled, so anything you would normally do can be done.Uncounted
P
15

There is another way to discard last pull

git reset --keep HEAD@{1}
Po answered 25/10, 2018 at 4:53 Comment(0)
C
7

I've used below command to revert the last commit

git merge --abort
Chilt answered 1/4, 2020 at 8:18 Comment(0)
S
5

git reset HEAD^ should take you to the previous commit. See here for more info.

Skewbald answered 17/4, 2012 at 18:15 Comment(3)
It's actually git reset --hard HEAD^, unless you don't want to touch your working directory.Scoop
According to the question the repo just needs to rollback after new a pull causes an issue, so the working directory should be empty.Skewbald
This is the right command -- it will get the last commit back, i.e. it will unstage all the files you committed last time. But the project will still run on the new code. I ran "git reset --hard" after this to get rid of all the pulled changes, and my project returned to normal. Thank you, Josh.Rawls
R
5

In this case it can make sense to use a branch, which can be easily removed or merged upon failure or success. This will help you keep track of what is new, and make things cleaner if you have more complicated cases than "just remove last commit" (especially since you want to do that with a script). So on your server:

git fetch --all           # fetch new commits from remote
git checkout -b testing   # create and switch to branch 'testing'
git merge origin/master   # merge new commits from remote branch master
                          # (in branch 'testing')

... then test stuff... if success:

git checkout master       # switch back to master
git merge testing

and upon failure:

git checkout master
git branch -D testing     # remove testing branch

But anyway... It your only point is to remove last commit, you can use git reset as pointed out by Josh.

Racklin answered 17/4, 2012 at 19:19 Comment(1)
Good point here, it's not what I was thinking about but could work very well. I will try and go with the simplest method.Lancer
B
4

The accepted answer by @Karl Bielefeldt didn't exactly worked for me. I am using GIT version 2.10.0.windows.1 May be this worked for older versions. I am getting "unknown switch 'e'" error. Finally, I did some changes and it worked.

Below are the steps to revert to state before previous pull:

  1. Use git reflog to see the list as Karl mentioned.
  2. Pick the commit version from the list to which you want to move back.
  3. Execute git reset --hard <commit version>
Brush answered 13/12, 2016 at 13:19 Comment(0)
B
3

You can simply do like this:

git reset --hard 8b2574f

where 8b2574f is found by doing

git reflog

8b2574f represents any HEAD and also mine is different from yours. You'll get yours when you run the above command.

Brigitte answered 6/12, 2020 at 10:36 Comment(0)
E
0

Another way of doing this would involve using tags. The thought being you could tag your HEAD with some version number prior to the pull, do the pull, then if you need to roll the HEAD back you can do git reset {tag name} That way if the pull has multiple commits you can skip back to where you were prior to the merge.

Eleonoreleonora answered 17/4, 2012 at 18:19 Comment(0)
V
-1

There are two commands :

  1. git reset --hard@{1}

  2. git reset --hard^1

First : rolls back to state before last pull.

second: rolls back to state before last commit ( includes merged code ).

Hope it helps.

Verger answered 20/3, 2020 at 18:23 Comment(1)
These commands didn't work for me, but did when I added HEAD like git reset --hard HEAD@{1} .Blight

© 2022 - 2024 — McMap. All rights reserved.