How to Revert All Local Changes in Git Managed Project to Previous State
There might be cases when you made several local changes and then realize that you want to get back to the original state. In this snippet we will suggest solution to this question. You can also check our How to Remove Local Files from the Current Working Tree snippet to understand the principles of reverting.
To revert changes that made to your working copy, execute git checkout:
git checkout .
If you want to revert changes made to the staging area run git reset:
If you want to revert already committed change, then run git revert like this:
git revert <commit 1> <commit 2>
To remove untracked files, which includes new files, generated files, etc. execute git clean with -f. :
git clean -f
Or use -fd to delete untracked directories including new or automatically generated directories:
git clean -fd
The -f or --force option deletes untracked files from the current directory, except the untracked folders or files specified with .gitignore.
The git revert Command¶
The git revert command an undo command, which reverts the changes introduced by the commit and attached a new git commit with resulting reversed content. Reverting is used to apply the inverse commit from the project history. The command is used for automatically going back and making fixes.
The git clean Command¶
The git clean is known as an undo command which completes the git reset and git checkout commands. Cleaning the working tree by recursively removing files that are not under version control, starts from the current directory. It operates on untracked files, whereas the other commands that work on files already added to the Git tracking index.