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:
Step 2: Create New Local Repository
Create a new local repository in the Git root directory using the “mkdir” command:
Step 3: Navigate to Newly Created Repository
Now, run the provided command and move to the newly created local repository:
Step 4: Create New Text File
Next, create a new text file through the “touch” command:
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:
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:
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:
Step 8: Track Newly Created File to Staging Area
Add the created file to the staging area by utilizing the following command:
Step 9: Check Status
Now, run the “git status .” command to check the current repository 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:
Step 11: Check Git Log History
Check the Git log reference log history using the “git log .” command:
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:
Step 13: Verify Reverted Commit
Lastly, execute the “git log .” command to ensure the reverted commit through SHA hash:
That’s all! We have explained the method to commit by an SHA Hash in Git.
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.