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!



Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s