The word “revert” has a unique meaning in Git. Without changing the commit history, you can restore the files in your repository to a former state by using the git revert command. To do this, fresh commits are made that perform the opposite action of previous commits, i.e., adding files and lines that were removed and deleting lines and files that were added.
To revert the most recently created commit, you can specify its hash or use HEAD:
git add .
git commit -m "This commit is a mistake"
git revert HEAD # will create a new commit doing the opposite of the one above
You can choose a range, from oldest to newest, to undo several recent changes. For every commit that is retracted, a new commit will be made.
git revert HEAD~2...HEAD # revert the last two commits
Restoring a prior state while keeping the repository’s edit history is possible with git revert. But sometimes you might want to remove past commitments instead of undoing them. Git reset --hard, with the commit to return to specified, can be used to accomplish this:
git reset --hard HEAD~
This will return the repository’s files to their previous state and remove the most recent commit from the current branch’s history.

