69

Hey I'm new to git and I need to undo a pull, can anyone help?!? So what I've done is...

  1. git commit
  2. git stash
  3. git pull --rebase
  4. git stash pop

this created a bunch of conflicts and went a bit wrong. Now doing 'git stash list' reveals that my stash is still there. Is it possible to revert my repo back to the point just after doing git commit. So effectively my repo only contains only changes I have made and nothing new from the server?

Nakilon
  • 34,866
  • 14
  • 107
  • 142
Thomas
  • 983
  • 2
  • 7
  • 15

4 Answers4

168

Actually, to make this easier Git keeps a reference named ORIG_HEAD that points where you were before the rebase. So, it's as easy as:

git reset --hard ORIG_HEAD
Pat Notz
  • 208,672
  • 30
  • 90
  • 92
  • 8
    I almost lost a week's worth of work, but this resurrected it. That's what happens when somebody else rebases your branch and force pushes to origin without pulling your changes first... This is definitely important to know if you work on any project with others where they force everyone to do rebasing instead of merging. Screw a cleaner history, rebasing is dangerous. – Gavin Apr 15 '15 at 15:35
  • This was the answer I needed. – Obinna Nnenanya May 24 '19 at 09:22
  • 1
    This is a much better way than the accepted answer using reflog. – Ka Mok Jan 24 '20 at 16:35
  • 1
    This answer adds some more detail: https://stackoverflow.com/a/10907222/5124002 – jciloa Mar 17 '20 at 13:53
53

using git reflog you will see a list of commits HEAD pointed to in the past

using

git checkout -b after-commit HEAD@{1} # or the commit you want to recover

you create a new branch at that precise position and check it out

knittl
  • 246,190
  • 53
  • 318
  • 364
  • doing this throws an error informing me that a file will be overwritten by merge. Is there a way to ignore this? – Thomas Feb 06 '10 at 14:21
  • 7
    Make sure your working directory is clean (git reset --hard HEAD will do that). Also, make sure the rebase is no longer in progress (git rebase --abort). – Wayne Conrad Feb 06 '10 at 14:22
  • You my friend are a life saver! Thank you :) – Thomas Feb 06 '10 at 14:46
  • 40
    Been there, done that! I think I've made every goof you can make with git. Git is a saw with no guard that makes it easy to cut your own arm off. But it also comes with an easy arm reattachment kit, and you can even attach the arm to your knee if you want. – Wayne Conrad Feb 06 '10 at 14:58
  • You can combine the checkout and reset commands and just do `git reset --hard HEAD@{1}` – Pat Notz Feb 08 '10 at 14:07
  • @pat: that will also move the branch, using reset and checkout separately you still have your old branch (which is a little bit safer if you don't know what you'r doing) – knittl Feb 08 '10 at 16:21
  • @wayne-conrad, can I quote you on "...attach the arm to your knee", please? – Gonen Dec 31 '13 at 15:40
  • @Gonen, I'm honored. Please do. – Wayne Conrad Dec 31 '13 at 16:35
8

You should checkout the command

git reset --merge

That eliminates the need for a git commit; git stash before a pull (Don't know about rebase though)

The command returns a workspace with uncommitted changes to the state before a conflicting pull.

0

Use git log -g and find the commit index you want to go back to, the just do git checkout index

Johan Dahlin
  • 25,300
  • 6
  • 40
  • 55
  • This doesn't actually help, since `pull --rebase` plays back your commits on top of what you just pulled; in this case, you can't just checkout your old commit, since it's on top of the commits you just pulled. – Achal Dave Apr 30 '13 at 01:42