Git

Revert to a Commit by a SHA Hash in Git

Git provides multiple functionalities for performing different operations, such as creating files, folders, and branches and then track them to the staging area from the working directory. These tracked changes can be saved in the repositories. Additionally, users are permitted to revert the added changes whenever they are required through several commands, and the “$ git reset <sha-hash&gt;” command is one of them.

This blog explains the method to revert a commit by a SHA Hash in Git.

How to Switch Back/Revert to a Commit by an SHA Hash in Git?

For the purpose of reverting to a commit through the SHA hash in Git, we will first create and track files, then perform the reverting operation. To do so, first, move to the Git root directory. Then, create a new Git local repository and navigate to it. Next, create a new file in the newly created repository and track it to the staging area from the working directory. After that, update the repository and view the reference log history. Copy the commit SHA hash and execute the “$ git reset <sha-hash>” command.

Let’s move ahead and understand the above-discussed procedure through the implementation!

Step 1: Move to Root Directory
First of all, execute the “cd” command and navigate to the Git root directory:

$ cd "C:\Users\nazma\Git"

Step 2: Create New Local Repository
Create a new local repository in the Git root directory using the “mkdir” command:

$ mkdir Test_7

Step 3: Navigate to Newly Created Repository
Now, run the provided command and move to the newly created local repository:

$ cd Test_7

Step 4: Create New Text File
Next, create a new text file through the “touch” command:

$ touch file1.txt

Step 5: Add Newly Created File to Staging Area
After creating a new text file in the working directory, run the “git add” command to track it in the staging area:

$ git add file1.txt

Step 6: Update Changes
Now, update the repository through committing and save in it, run the “git commit” command along with the “-m” option to add the desired commit message:

$ git commit -m "file1.txt added"

Step 7: Create New Text File
Now, create another text file in the repository by executing the “touch” command and specifying the new file name:

$ touch file3.txt

Step 8: Track Newly Created File to Staging Area
Add the created file to the staging area by utilizing the following command:

$ git add file3.txt

Step 9: Check Status
Now, run the “git status .” command to check the current repository status:

$ git status .

As you can see, the recently created file is placed in the staging area and is ready to commit to the repository:

Step 10: Commit Changes
Run the “git commit” command to commit the staged file into the repository:

$ git commit -m "file3.txt added"

Step 11: Check Git Log History
Check the Git log reference log history using the “git log .” command:

$ git log .

From the below-listed output, we will copy the SHA hash of the desired commit to which we want to revert:

Step 12: Reset HEAD With SHA Hash
Now, run the “git reset” command with the copied SHA hash to revert the commit:

$ git reset 061de25

Step 13: Verify Reverted Commit
Lastly, execute the “git log .” command to ensure the reverted commit through SHA hash:

$ git log .

That’s all! We have explained the method to commit by an SHA Hash in Git.

Conclusion

To revert to a commit through the SHA hash in Git, first, navigate to the Git root directory and create a new local repository. Then, navigate to the newly created repository, create, and add a new text file. Next, commit changes and check the reference log history. Copy the commit SHA hash and run the “$ git reset <sha-hash>” command. This blog described the procedure to commit by an SHA Hash in Git.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.