5

I just did a pull from github and pulled from the wrong github repository into my project. How do I undo this?

Thanks!

Barka
  • 8,764
  • 15
  • 64
  • 91

3 Answers3

3

One way to do this

$ git reset --hard latest-good-commit-hash

Use git log --all (or more graphically, use gitk --all) to see the commit history

fajran
  • 2,769
  • 22
  • 13
3

Try git reflog. Reflog allows you to undo almost any operation. Something like this

git reset --hard HEAD@{1}

However, if you had dirty working copy, it will erase all your changes, so beware.

Piotr Findeisen
  • 19,480
  • 2
  • 52
  • 82
  • Thanks! I had added and committed but not pushed. what was added and committed would not be over ridden by this now would it? – Barka Mar 17 '12 at 23:12
  • That's correct, if you had added and committed, what you had added and committed will still be there after this. – Mike Morearty Mar 18 '12 at 00:07
  • @user277498, see also output of `git reflog` before you do the `reset`. It contains information what the heck the `@{1}` suffix really means, there needs to be no magic :) – Piotr Findeisen Mar 18 '12 at 00:25
3
git reset --hard HEAD^

This command resets your branch to the previous commit, that is the commit before the merge commit, which is the one you want to undo. Your work will be left untouched.

Remember that everything that has been commited is still there as loose objects and you can always use git reflog to revert to any version HEAD has pointed to before. This is why it's so important to commit often with git, you can undo all changes you have done with ease.

rtn
  • 127,556
  • 20
  • 111
  • 121
  • 1
    All these answers using `git reset --hard _some local commit_` make your repository look OK, but leave all the commits from the wrong remote repository in your local repository, so none is a complete "undo". git gc will not delete the wrong commits because they are in your reflog (since you referred to them recently). I think they will eventually expire, but if you want to kill them right away it requires deeper surgery. – skierpage Jan 28 '13 at 20:27
  • 1
    @skierpage Correct. The commits are still there, dangling, until you clean them up. There are various ways of doing this. The easiest way is to re-clone the repository (assuming you can throw away the one you are using right now) – rtn Jan 28 '13 at 23:18