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. Here, we will suggest a 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.
Reverting local changes
Reverting the unstaged changes
If you have any unstaged changes in your working copy, then you can revert them by running the git checkout command:
git checkout .
Reverting the staged changes
If you want to revert changes made to the staging area, then run the git reset command to bring them back from the staging area:
git reset
After running this command, you need to run the git checkout command to revert all the local changes as described in the last section.
Reverting the committed changes
If you want to revert already committed changes, then run the git revert command:
git revert <sha1-commit-hash>
Reverting the untracked files
To remove the untracked files (including the newly-created files), run git clean with the -f option:
git clean -f
If you have untracked directories along with the untracked files, then you can revert by adding the -d option:
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.