This study will elaborate on how to undo Git reset with the –hard flag.
How to Undo Git Reset With the –hard Flag?
Suppose you have some of your project files in the Git directory. You open them and make changes. After that, commit the added change to the repository. Now, to reset changes, execute the “$ git reset –hard <commit-ref>” command.
The below-given procedure will demonstrate the stated concept.
Step 1: Navigate to Git Directory
Move to the Git directory utilizing the “cd” command:
Step 2: List Directory Content
List the Git repository content using the “ls” command:
Step 3: Update File
Next, execute the “start” command to open and update the specified file:
Here, “myfile2.html” will be opened in the Git default editor. Make changes, save them, and exit:
Step 4: Commit Changes
Now, save the changes in the remote Git repository with the help of the provided command with any commit message:
Step 5: Check Log History
Execute the “git log” command to check the log history:
Here, we have specified the “-2”, which indicates the range for showing commit log. As a result, we will copy the previous commit reference:
Step 6: Reset Commit
Now, execute the “git reset” command with the “–hard” flag with copied commit ref:
Below output indicates that we have successfully reset the Git HEAD to the previous commit:
Let’s check out the next section to reset the uncommitted changes with –hard flag.
How to Undo Git Reset Not Committed Changes With –hard flag?
There can be a situation where a Git user wants to reset the uncommitted changes with the –hard flag. It indicates that the added changes exist in the Git staging area but are not committed to the repository for saving purposes. To understand this scenario, follow the provided steps.
Step 1: List Dangling Commit Hashes
First, list all dangling commit hashes with the provided command:
As you can see, the dangling commit exists at the end. Now, copy its commit ref to the clipboard:
Step 2: Show Dangling Commit Content
Run the “git show” command to show what dangling commit hash hold:
the whole history of dangling commit hash will be now displayed:
Step 3: Check Log History
Check the log history of the Git repository utilizing provided command:
From the displayed output copy the ref of commit to which you need to reset the HEAD:
Step 4: Reset Git HEAD
Execute the “git reset” command with the “–hard” flag and the commit ref:
As you can see our HEAD is reset to the previous commit successfully:
That’s it! We have compiled the easiest method for undoing Git reset with the –hard flag.
Conclusion
To undo Git reset with the –hard flag, open the Git terminal and move to the Git directory. Then, list the content of the current repository. Open and update the required file. After that, commit changes, check log history, and run the “$ git reset –hard <commit-ref>” to undo the Git reset. You can also reset the uncommitted changes which exist in the staging area. This study learned how to undo Git reset with the –hard flag.