Source Control · Web Development

How to recover uncommitted changes after a git reset hard ?

It happens quite often that git reset –hard if not used carefully that a few hours or even days of work get lost. A rule of thumb is not to use it unless absolutely necessary. Another rule is to commit often on your local repository.

However, just in case it happens, don’t worry, there’s a remedy. Type the following command:

$ git reflog

In fact git maintains uncommitted changes on a parallel Directed Acyclic Graph (DAG) for internal bookkeeping. The previous command should show the Head states previous to running git reset –hard. Just pick the most recent Head that matches your changes that you want to recover. Afterwards, reset the current Head to that previous Head using:

$ git reset –hard Head@{4}

And wallah, your unsaved changes are fully recover, just commit, and get going!

References:
http://stackoverflow.com/questions/5473/how-can-i-undo-git-reset-hard-head1/21778#21778
http://stackoverflow.com/questions/7374069/undo-git-reset-hard-with-uncommitted-files-in-the-staging-area/7376959#7376959

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