Git

Reverting to a Specific Commit Based on Commit id with Git

Git is a well-liked DevOps projects version control tool that is specifically used to manage the different versions of projects and their source code. Git users modify projects by implementing some modifications, and sometimes they do not like them or may cause some problems, such as crashing the application. Therefore, it is necessary to revert previous or specific commits to restore the project’s old version.

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:

$ cd "C:\Git"

Step 3: Generate New File
Generate a new text file by utilizing the β€œtouch” command:

$ touch File1.txt

Step 4: Track New File
Next, add the newly created file to the tracking index list (staging area) using the β€œgit add” command:

$ git add .

Verify whether the file is added to the tracking index or not:

$ git status

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:

$ git commit -m "New commit"

Check if the changes are committed or not, view the Git repository logs:

$ git log

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:

$ git reset --hard ab9256c

Check the repository logs once more to see the commits and confirm whether we have reverted to the specific commit or not:

$ git log

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.

About the author

Rafia Zafar

I am graduated in computer science. I am a junior technical author here and passionate about Programming and learning new technologies. I have worked in JAVA, HTML 5, CSS3, Bootstrap, and PHP.