How to revert a Git repository to a previous commit
In this tutorial, we are going to learn about how to undo the git repository to a previous commit.
Reverting git to a previous commit (locally)
If your commit is not pushed to a remote git repository and you are trying to revert it to a previous commit follow the below steps.
-
Inside the command line navigate to a repository, you want to undo it back to a previous commit.
-
Run the
git revert
command followed by theHEAD
reference.
git revert HEAD
This above command will create a new commit based on the previous commit, it doesn’t change any previous commit history.
Note:
Head
is referencing to a last commit in the current branch
Reverting git to a previous commit (remotely)
If your commit is pushed to a remote git repository and you are trying to revert it to a previous commit.
-
Follow the above two steps
-
Now, push your repository to remote by running the
git push
command followed by the-f
flag.
git push -f