Git

How to Undo Git Reset With the –hard Flag

In Git, sometimes, users commit changes after updating files of team projects. After that, they may want to get the previous version of the files and reset the HEAD to a particular commit. Git users can perform these actions by utilizing the “$ git reset” command with the “–hard” flag, which is used to discard uncommitted changes on the Git local system.

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:

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

Step 2: List Directory Content
List the Git repository content using the “ls” command:

$ ls

Step 3: Update File
Next, execute the “start” command to open and update the specified file:

$ start myfile2.html

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:

$ git commit -m "myfile2.html updated"

Step 5: Check Log History
Execute the “git log” command to check the log history:

$ git log -2

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:

$ git reset --hard 553734718de8a2a1c20560c8ae0c734b509c34c2

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:

$ git fsck --lost-found

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:

$ git show d8b43a87dbec0142583c2a7074d892d46f314f31

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:

$ git log -2

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:

$ git reset --hard 553734718de8a2a1c20560c8ae0c734b509c34c2

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.

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.