Git rollback 1 pull

GitRollback

Git Problem Overview


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.

Git Solutions


Solution 1 - Git

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.

Solution 2 - Git

There is another way to discard last pull

git reset --keep HEAD@{1}

Solution 3 - Git

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

Solution 4 - Git

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.

Solution 5 - Git

I've used below command to revert the last commit

git merge --abort

Solution 6 - Git

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>

Solution 7 - Git

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.

Solution 8 - Git

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.

Solution 9 - Git

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBastianView Question on Stackoverflow
Solution 1 - GitKarl BielefeldtView Answer on Stackoverflow
Solution 2 - GitRiajul IslamView Answer on Stackoverflow
Solution 3 - GitJosh FarnemanView Answer on Stackoverflow
Solution 4 - GitFrançoisView Answer on Stackoverflow
Solution 5 - Gitanjaneyulubatta505View Answer on Stackoverflow
Solution 6 - GitJoggerView Answer on Stackoverflow
Solution 7 - GitBlessingView Answer on Stackoverflow
Solution 8 - GitGreg DemetrickView Answer on Stackoverflow
Solution 9 - GitomkarView Answer on Stackoverflow