Bastian
Bastian

Reputation: 5855

Git rollback 1 pull

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.

Upvotes: 66

Views: 135614

Answers (9)

Blessing
Blessing

Reputation: 2720

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.

Upvotes: 4

anjaneyulubatta505
anjaneyulubatta505

Reputation: 11685

I've used below command to revert the last commit

git merge --abort

Upvotes: 7

omkar
omkar

Reputation: 216

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.

Upvotes: -1

Riajul Islam
Riajul Islam

Reputation: 1483

There is another way to discard last pull

git reset --keep HEAD@{1}

Upvotes: 15

Jogger
Jogger

Reputation: 447

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>

Upvotes: 5

Karl Bielefeldt
Karl Bielefeldt

Reputation: 49088

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.

Upvotes: 165

Fran&#231;ois
Fran&#231;ois

Reputation: 8138

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.

Upvotes: 5

Greg Demetrick
Greg Demetrick

Reputation: 759

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.

Upvotes: 0

Josh Farneman
Josh Farneman

Reputation: 1739

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

Upvotes: 5

Related Questions