GIT recovering from git reset –hard

Today I accidentally deleted my changes on git with the git reset <commit> –hard command and I read in a few places that you cannot recover from this. Well that’s a LIE. Recovering from a hard reset turns out to be quite simple:

Just run git reflog

This should list a bunch of recent versions which git still knows about. The output should look something like this:

50dbb7c HEAD@{0}: checkout: moving from master to 50dbb7c
f29e4a3 HEAD@{1}: HEAD~1: updating HEAD
79c95b2 HEAD@{2}: HEAD~1: updating HEAD
50dbb7c HEAD@{3}: HEAD~1: updating HEAD

My issue was that I had run ‘git reset HEAD~1 –hard’ a few times and I foolishly went down one too many heads, thus deleting my changes.

To get your changes back simply run git checkout <version_hash>, e.g. git checkout 79c95b2 in my case. If it gives you trouble, just use the -f flag: I.e. git checkout <version_hash> -f

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s