How to Revert All Local Changes in Git Managed Project to Previous State
Read this tutorial and know how you can solve your problem of reverting all local changes in Git managed project to previous state with most used commands.
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 restore command:
git restore .Reverting the staged changes
If you want to unstage changes, run the git restore command:
git restore --staged .Note that this only removes changes from the staging area. To discard working tree changes as well, run git restore . as described in the previous 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 <kbd class="highlighted">-f</kbd> option:
git clean -fIf you have untracked directories along with the untracked files, then you can revert by adding the <kbd class="highlighted">-d</kbd> option:
git clean -fdThe <kbd class="highlighted">-f</kbd> or <kbd class="highlighted">--force</kbd> 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 is an undo command that reverts the changes introduced by a commit and attaches a new git commit with the 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 <kbd class="highlighted">git clean</kbd> command is known as an undo command that completes the <kbd class="highlighted">git reset</kbd> and <kbd class="highlighted">git checkout</kbd> 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 work on files already added to the Git tracking index.