This article will demonstrate the method for reverting to a specific commit based on the commit id in Git.
How to Revert to Specific Commit Using Commit Id in Git?
To revert to a specific commit using commit id, first, open the Git local repository and commit some changes. After that, revert these changes and move back to a specific commit through the βgit reset –hard <commit-id>β command.
To do so, check out the mentioned instructions.
Step 1: Open Git Terminal
From the Start menu, first, open the Git Bash terminal:
Step 2: Open the Git Repository
Open the Git repository through the βcdβ command:
Step 3: Generate New File
Generate a new text file by utilizing the βtouchβ command:
Step 4: Track New File
Next, add the newly created file to the tracking index list (staging area) using the βgit addβ command:
Verify whether the file is added to the tracking index or not:
It can be observed that the new file is added to the repository tracking index(staging area) successfully:
Step 5: Commit Tracked Changes
Next, commit the staged changes by utilizing the βgit commitβ command. Moreover, to embed the message along with the commit, utilize the β-mβ option:
Check if the changes are committed or not, view the Git repository logs:
The below output indicates that the changes are committed successfully. Now, note the commit id to which you want to revert:
Step 6: Revert to Specific Commit
Revert to a specific commit by using the βgit reset –hardβ command and providing the commit id to which you want to revert:
Check the repository logs once more to see the commits and confirm whether we have reverted to the specific commit or not:
Here, you can see that we have successfully reverted to a specific commit based on the commit id:
Here you go! You have learned the method for reverting to a specific commit through commit id in Git.
Conclusion
To revert to a specific commit by using the commit id, first, open the Git repository. Next, make some changes and commit them using the β$ git commitβ command. After that, to revert changes or move back to a specific commit, utilize the β$ git reset –hard <commit-id>β command. This write-up has demonstrated the method for reverting to a specific commit based on the commit id in Git.