This guide will demonstrate the method to roll back/reset a Git repository to a specific commit.
How to Roll Back/Reset a Git Repository to a Specific Commit?
To reset a Git repository to the desired commit, follow the provided steps:
- Redirect to the local repository.
- View commits history.
- Choose a particular commit and its copy SHA-hash.
- Run the βgit reset –hard <commit-id>β command.
- Verify changes.
Step 1: Switch to Git Directory
First, type out the below-provided command to redirect to a desired local directory:
Step 2: Check Git Commit History
Then, view the current position of HEAD by checking the log history using the βgit logβ command:
The below screenshot indicates that the HEAD is pointing to the βFileD addedβ commit. Now, choose and copy the SHA-hash value of the commit that is required to reset. For instance, we have selected the βFiles addedβ commit, whose hash value is β9bd11a3β:
Step 3: Reset to Particular Commit
Now, execute the βgit reset –hardβ command along with the copied commit hash and move the HEAD pointer to it:
Here, the β–hardβ option is used to roll back to the desired commit:
Step 4: Verify Git Log
Lastly, view the commit history to check the current position of HEAD:
In the below-given screenshot, it can be observed that the HEAD is now pointing to the specified β9bd11a3β commit:
We have explained the procedure of rolling back a Git repository to a desired Git commit.
Conclusion
To roll back/reset a Git repository to the specific commit, first, move to the local Git directory. Then, view the commit history and select the desired commit. Lastly, run the βgit reset –hard <commit-id>β command to roll back the Git repository to the desired commit. This guide demonstrated the easiest way to reset a Git repository to a particular commit.