Git
How to Rollback a Git Repository to a Previous Commit
February 28, 2023
This guide is part of the "Snippets" series. This series is focused on providing simple and accessible tutorials on various topics relating to development!
To rollback a Git repository to a previous commit, you can use the git reset
command along with the hash of the commit you want to revert to. This will reset the current branch to the specified commit and remove any subsequent commits.
Here are the steps to rollback a Git repository to a previous commit:
Find the hash of the commit you want to rollback to by using the git log
command. This will display a list of commits along with their hashes.
$ git log
Copy the hash of the commit you want to rollback to.
Use the git reset
command along with the hash of the commit to reset the repository to that commit.
$ git reset --hard <commit-hash>
Verify that the repository has been rolled back to the specified commit by using the git log
command again.
$ git log
Note that git reset --hard
will remove any changes in the working directory that are not committed. If you want to keep those changes, you can use git stash
to temporarily save them before running git reset
, and then use git stash apply
to restore them afterwards.
External resources:
- Git documentation on resetting to a previous commit: https://git-scm.com/book/en/v2/Git-Tools-Reset-Demystified
- Atlassian's tutorial on reverting a Git repository to a previous commit: https://www.atlassian.com/git/tutorials/undoing-changes/git-revert