Recovering from a lost commit

In case you have reverted back to a past commit and lost a newer commit you can recover the lost commit by running

git reflog

Then find your lost commit, and reset back to it by doing

git reset HEAD --hard <sha1-of-commit>

Restore a deleted file after a commit

In case you have accidentally commited a delete on a file and later realized that you need it back.

First find the commit id of the commit that deleted your file.

git log --diff-filter=D --summary

Will give you a sorted summary of commits which deleted files.

Then proceed to restore the file by

git checkout 81eeccf~1 <your-lost-file-name>

(Replace 81eeccf with your own commit id)

Restore file to a previous version

To restore a file to a previous version you can use reset.

git reset <sha1-of-commit> <file-name>

If you have already made local changes to the file (that you do not require!) you can also use the --hard option

Recover a deleted branch

To recover a deleted branch you need to find the commit which was the head of your deleted branch by running

git reflog