Git – How to revert all local changes in Git managed project to previous state

gitgit-checkoutrevert

I have a project in which I ran git init.
After several commits, I did git status which told me everything was up to date and there were no local changes.

Then I made several consecutive changes and realized I wanted to throw everything away and get back to my original state. Will this command do it for me?

git reset --hard HEAD

Best Answer

If you want to revert changes made to your working copy, do this:

git checkout .

If you want to revert changes made to the index (i.e., that you have added), do this. Warning this will reset all of your unpushed commits to master!:

git reset

If you want to revert a change that you have committed, do this:

git revert <commit 1> <commit 2>

If you want to remove untracked files (e.g., new files, generated files):

git clean -f

Or untracked directories (e.g., new or automatically generated directories):

git clean -fd