Undo a git reset --hard origin/master

chrisjleu picture chrisjleu · Nov 13, 2014 · Viewed 18.6k times · Source

Working on local master branch:

git commit -m "Lots of important commits"
git reset --hard origin/master

How can I retrieve the commits that have been lost as a result of the git reset (from the remote)?

EDIT: note this is not about retrieving uncommitted changes.

Answer

Gabriele Petronella picture Gabriele Petronella · Nov 13, 2014

If you committed it, nothing is lost.

If you have the reference of the commit, you can just git reset --hard <sha> to that precise commit.

In case you don't you can always use git reflog to retrieve the sha before performing the hard reset.

For instance if git reset --hard origin/master is the last command you run, you can do

git reset HEAD@{1}