324

I would like to undo my git pull on account of unwanted commits on the remote origin, but I don't know to which revision I have to reset back to.

How can I just go back to the state before I did the git pull on the remote origin?

2
  • 8
    Side note: you may find it useful to git fetch upstream first, then take a quick look at git diff upstream/branch to see what you will be merging in. If all is well, then proceed with git merge upstream/branch
    – Shahbaz
    Commented Oct 17, 2013 at 15:51
  • 6
    You will lose all your street cred and be docked a week's pay if any hipster brogrammer sees you do git commands from a GUI, but both GitHub Desktop and Atom have safe, straightforward buttons to undo commits and checkboxes to easily and clearly stage and unstage files. GUIs are people too! Commented Jun 15, 2019 at 6:25

8 Answers 8

496

Or to make it more explicit than the other answer:

git pull 

whoops?

git reset --keep HEAD@{1}

Versions of git older than 1.7.1 do not have --keep. If you use such version, you could use --hard - but that is a dangerous operation because it loses any local changes.


To the commenter

ORIG_HEAD is previous state of HEAD, set by commands that have possibly dangerous behavior, to be easy to revert them. It is less useful now that Git has reflog: HEAD@{1} is roughly equivalent to ORIG_HEAD (HEAD@{1} is always last value of HEAD, ORIG_HEAD is last value of HEAD before dangerous operation)

23
  • 2
    what is the difference between HEAD@{1} and HEAD^ ?
    – hugemeow
    Commented Aug 28, 2012 at 6:11
  • 8
    @hugemeow That would be a fine SO question. Meanwhile man git-rev-parse describes this. HEAD@{1} is the previous value of symbolic HEAD in the reflog whereas HEAD^ is the (first) parent revision of the current HEAD. These two need nod be equivalent (e.g. after a rebase, a hard reset, a branch switch and such things). Do read the linked article for reflog. Cheers
    – sehe
    Commented Aug 28, 2012 at 7:34
  • 13
    PowerShell users, escape the brackets with a backtick: git reset HEAD@`{1`} Commented Feb 15, 2013 at 15:33
  • 6
    ss64.com/ps/syntax-esc.html I think you have wanted to type HEAD@`{1`}, or for that matter do what works on POSIX shells too: 'HEAD@{1}'
    – sehe
    Commented Feb 15, 2013 at 15:38
  • 2
    I think it not only reset the pull, but also my commits =(
    – falsarella
    Commented Apr 24, 2015 at 23:41
85

git reflog show should show you the history of HEAD. You can use that to figure out where you were before the pull. Then you can reset your HEAD to that commit.

5
  • git reflog show gave this output: c9e5e4d HEAD@{0}: pull : Fast forward 1c86a22 HEAD@{1}: pull origin master: Fast forward 05c141a HEAD@{2}: pull : Fast forward Can I safely reset the HEAD to HEAD@{1}
    – Kartins
    Commented Apr 28, 2011 at 9:32
  • The other answer by sehe has details on how to get there. Commented Apr 28, 2011 at 9:37
  • This was super-useful after a disastrous commit somehow interspersed merge commits into my history. Got them out by looking for a last-known good in the reflog and then force pushing.
    – Domenic
    Commented May 7, 2012 at 18:42
  • What if pull is the first action? If pull is at HEAD@{1}, and nothing else before that, how do you revert to a state before that?
    – O.O
    Commented Jul 16, 2013 at 17:17
  • Recreate the repository? Commented Jul 17, 2013 at 5:49
49

This worked for me.

git reset --hard ORIG_HEAD 

Undo a merge or pull:

$ git pull                         (1)
Auto-merging nitfol
CONFLICT (content): Merge conflict in nitfol
Automatic merge failed; fix conflicts and then commit the result.
$ git reset --hard                 (2)
$ git pull . topic/branch          (3)
Updating from 41223... to 13134...
Fast-forward
$ git reset --hard ORIG_HEAD       (4)

Checkout this: HEAD and ORIG_HEAD in Git for more.

22

Find the <SHA#> for the commit you want to go. You can find it in github or by typing git log or git reflog show at the command line and then do git reset --hard <SHA#>

3
  • Find the what for the commit?
    – Martin
    Commented Aug 28, 2015 at 16:44
  • Sorry, I fixed that <SHA#>
    – Nina
    Commented Aug 28, 2015 at 18:10
  • This is the command I use the most when I have to undo something
    – Kartins
    Commented Sep 17, 2015 at 14:05
11

to undo git pull

git reset --hard HEAD^

takes your local repo back to previous commit state. (Note: HEAD^ means the first immediate parent of the tip of the current branch. HEAD^ is short for HEAD^1, and you can also go further up if you want (e.g. HEAD^2 , HEAD^3 ...). after reset, if some unknown files left (files that arrived as a result of original git pull), they will be listed as untracked files and you can clean them up using

  git clean -fd
10

Even though the above solutions do work, This answer is for you in case you want to reverse the clock instead of undoing a git pull. I mean if you want to get your exact repo the way it was X Mins back then run this command

git reset --hard branchName@{"X Minutes ago"}

Note: before you actually go ahead and run this command please only try this command if you are sure about the time you want to go back to and heres about my situation.

I was currently on a branch develop, I was supposed to checkout to a new branch and pull in another branch lets say Branch A but I accidentally ran git pull origin A before checking out.

so to undo this change I tried this command

git reset --hard develop@{"10 Minutes ago"}

if you are on windows cmd and get error: unknown switch `e

try adding quotes like this

git reset --hard 'develop@{"10 Minutes ago"}'

4

From https://git-scm.com/docs/git-reset#Documentation/git-reset.txt-Undoamergeorpullinsideadirtyworkingtree

Undo a merge or pull inside a dirty working tree

$ git pull           (1)
Auto-merging nitfol
Merge made by recursive.
 nitfol               |   20 +++++----
 ...
$ git reset --merge ORIG_HEAD      (2)

Even if you may have local modifications in your working tree, you can safely say git pull when you know that the change in the other branch does not overlap with them.

After inspecting the result of the merge, you may find that the change in the other branch is unsatisfactory. Running git reset --hard ORIG_HEAD will let you go back to where you were, but it will discard your local changes, which you do not want. git reset --merge keeps your local changes.

See also https://stackoverflow.com/a/30345382/621690

0
4

Reset the master branch:

git reset --hard origin/master

Not the answer you're looking for? Browse other questions tagged or ask your own question.