Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How I undo some changes in my workspace and get back to my last commit? [duplicate]

Tags:

git

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?

like image 803
Enrichman Avatar asked Jun 29 '12 18:06

Enrichman


People also ask

How do you undo all changes to last commit?

In order to undo the last commit and discard all changes in the working directory and index, execute the “git reset” command with the “–hard” option and specify the commit before HEAD (“HEAD~1”).


3 Answers

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.

like image 179
Matt Enright Avatar answered Oct 22 '22 08:10

Matt Enright


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.

like image 44
Adam Dymitruk Avatar answered Oct 22 '22 06:10

Adam Dymitruk


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

like image 2
toske Avatar answered Oct 22 '22 07:10

toske