8

Possible Duplicate:
Git: Revert to previous commit status

I've tried some changes in my code but I messed up too many things (never work when you're tired and it's late) and I just want to go back to my last commit. I DIDN'T do git add or git commit and obviously if I do git pull everything is up to date.

I thought that git checkout was what I needed but it didn't worked.. any help?

Community
  • 1
  • 1
Enrichman
  • 11,157
  • 11
  • 67
  • 101

4 Answers4

13

The answers mentioning reset --hard will do what you want (as well as some other things that you may or may not), but you were correct in thinking that checkout was the command you needed. The problem is that checkout does two different things, so you need to supply it with a path argument:

git checkout .

from the root of your repository will do you just fine.

Matt Enright
  • 7,245
  • 4
  • 33
  • 32
  • 1
    Unless the problem is newly added untracked files, but it sounds like OP was having problems with *changes* more than *additions*. – blahdiblah Jun 29 '12 at 19:34
  • Well, I did also an addition but I just deleted the file. Small project fortunately! By the way, the command did the trick! I'll accept your answer but could you tell also me the differences between 'rebase' 'reset' and 'checkout'? I just know that 'stash' would make like a different local branch that later you can decide to keep or not (or something like that). – Enrichman Jun 29 '12 at 20:49
  • 3
    `reset` is a command that primarily operates on the *index*, and can operate on the working directory through some additional flags (`--hard` being one of them). `checkout` is a command that operates on your working directory, either by checking out the contents of a tree or updating a set of local paths (without touching the index). `rebase` is a way of rewriting part of your commit history (as a series of patches) to apply it to a different base (it doesn't really have anything to do with your question). – Matt Enright Jun 30 '12 at 18:20
7

DO NOT git reset -hard it is PERMANENT!

Please use

git stash -u 

instead! If you have a piece of work in there that you zapped by accident, you can still get it back. This never gets pushed to your remote unless you choose to do so by making a branch out of it and pushing it up.

Also, you are on the right track that you can use git checkout to accomplish the same thing. The syntax is git checkout HEAD -- .. But it has the same problem as git reset --hard. Stick with stash and you will save losing your hair in the future.

Longer answer

The above solutions revert all your changes. However, you asked how to get rid of some of the changes. I'll add this for completeness.

To do this, you can

git add file1 file2 file3
git stash save --patch

You will now be prompted for what exactly you want to make dissappear... down to what ever level of granularity. So you can safely "reject" only a few changes to a single file if you choose to do so.

Adam Dymitruk
  • 124,556
  • 26
  • 146
  • 141
  • 1
    that's not a reason to -1 O.o he is asking to delete it permanently. Now I do agree that git stash is better. to apply changes again just `git stash apply` – Hassek Jun 29 '12 at 18:36
  • 1
    @Adam, haha! -1 ing all the other answers is bad I guess, since he dint mention any permanent or temporary changes. ;) He just mentioned to get back to previous commit & they were right in that sense. Cheers!! – uday Jun 29 '12 at 18:37
  • 1
    Just a point of clarification: `reset --hard` is permanent, but it won't touch untracked files (which `stash -u` explicitly saves) -- so you aren't losing *those* with `reset --hard` in any case. – Matt Enright Jun 29 '12 at 18:38
  • un-downvoted.. was bitten by this recently and over-reacted :). @MattEnright correct (being extra safe). You can get the same effect as reset by leaving out the `-u` option. Later on you can clean up your stashes if you want. – Adam Dymitruk Jun 29 '12 at 18:40
2

git reset --hard this will remove all of your untracked changes to last commit in repository.

toske
  • 1,744
  • 13
  • 24
2

git reset --hard will discard all of your work, but you probably want to do:

git stash save "A bunch of stuff that didn't work"

on the off chance that you want to recover some of the changes.

William Pursell
  • 204,365
  • 48
  • 270
  • 300