Undo Git Reset Hard

Gburgos
2 min readApr 20, 2021

This is the nightmares of nightmares when it comes to losing everything you have worked on the past day, week, or month. At times we do use a GIT reset to reverse commits or undo actions but reset hard should never be used unless necessary.

Despite all being lost in your files or git log not showing your work, there is a SOLUTION.

Solution

Straight to it for all my stressed out developers, especially beginning GIT users like myself.

1st Step:

Move into the location of your git (master) folder in the terminal.

2nd Step:

Once you’re in the master folder, run: “git reflog”.

As git reflog runs, you will see all your previous HEAD -> MASTER commits as you will with running git log.

3rd Step:

Select the id of your previous HEAD, before the git reset hard was executed. The id will be located on the left, where you will see the yellow font of characters. Copy and paste that 7 character id of your commit.

Below is an image of step 1–3.

Last Step:

Once you’ve highlighted the commit id and copied the id. Run this command:

git reset — — hard <paste the id>

Below is the image of myself executing the command. Reference the id number pasted to see how it will look.

Below are text examples. Execute in this order on the terminal.

cd <Location of master folder>

git reflog

Find the commit of previous HEAD and select the id to copy/paste.

git reset — — hard <id>

Conclusion (Outcome of my experience)

Immediately after running the last command I opened Jupyterlab to ensure my project files were there and my codes where as I left it.

Good News! It was.

I found this solution through a blog written by Sara Ford, 2017. Dear Sara, if you’re reading this BLESS YOUR HEART!

I decided to re-blog this sort of solution because I promised myself I will, once my files were recovered. I felt I had to pay it forward and spread this knowledge further to help others like myself who ran a hard reset.

I attached the link to Sara Ford’s blog below.

Sara Ford’s Blog

How to recover from the “oh no! I did a git reset and now my files are gone!” — 087
If you don’t know about git reflog, your life is about to get much, much better. Prepare yourself. Git is about to make…saraford.net

--

--