How did I undo some changes in my workspace and get back to my last commit?

Possible duplicate:
Git: return to previous commit status

I tried some changes in my code, but I messed up too many things (never work when you are tired, and it's late), and I just want to get back to my last commit. I did not do git add or git commit , and obviously, if I do git pull , everything will be relevant.

I thought git checkout is what I need, but it didn't work .. any help?

+4
source share
4 answers

Answers that mention reset --hard will do what you want (as well as some other things that you may or may not), but you were right that checkout was the command you needed. The problem is that checkout does two different things, so you need to provide a path argument:

git checkout .

from the root of your repository will be just fine for you.

+8
source

NOT git reset -hard !

Please, use

 git stash -u 

instead of this! If you have a job that you accidentally classified, you can still return it. It never gets on your remote control unless you want to do it by pulling a branch out of it and pushing it.

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

Longer answer

The above solutions bring back all your changes. However, you asked how to get rid of some changes. I will add this for completeness.

To do this, you can

 git add file1 file2 file3 git stash save --patch 

Now you will be asked what exactly you want to do dissappear ... down to the level of detail. Thus, you can safely β€œreject” only a few changes to a single file if you decide to do so.

+4
source

git reset --hard this will delete all your unhandled changes to the last commit in the repository.

+1
source

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

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

if you want to restore some changes.

+1
source

All Articles