Lets say you have been working on a project that is tracked by Git and suddenly realized that you did something wrong.
You have some not yet committed changes and you want to undo everything.
In particular you want to delete all newly created files and folders and restore all modified or deleted.
In short you need to revert all changes back to the last commit.
Cool Tip: Want to look back to see what has happened with one particular file? Find out the commit history of this file, history of diffs and up to the history of renames! Read more →
If you are sure that you will never need the uncommitted changes, then run the below commands.
Reset All Changes
Unstage all files and directories you might have staged with
$ git reset --hard HEAD
Remove Unstaged Files And Directories
Good idea: Firstly run
git clean -n to preview files and directories that are going to be deleted to ensure you don’t need them anymore.
Cool Tip: Have forgotten the meaning of some term in Git? Not a problem! Simply read and bookmark this article! This article →
Preview and then remove untracked files and directories:
$ git clean -nd $ git clean -fdYou have just successfully removed files unknown to Git, but this could be not enough.
The above command doesn’t remove files that are listed in
For example some builds or temporary files that are usually not tracked by Git.
And of cause you probably would like to remove such files also.
Attention: Execution of the below command is basically equivalent to fresh
git clone from original source. It will REMOVE ALL LOCAL UNTRACKED FILES INCLUDING IGNORED BY GIT through
Preview and then remove ALL untracked files and directories:
$ git clean -ndx $ git clean -fdx