In Git, nothing is lost as long as you've committed it1.
You can use the reflog to get a journal of the commits that master
has been pointing to through time by saying:
git reflog master
Once you've located the latest one of your lost commits in the reflog, you can make master
point to it again by using git reset
.
For example, let's say that the latest lost commit is on the 4th entry in the reflog. In order to restore it on master
you would say:
git checkout master
git reset --hard master@{4}
where master@{4}
refers to the commit that master
was pointing to 4 entries ago from where it is now.
Note that before you reset master
, you should make sure that your working directory is clean, that is you don't have any uncommitted changes.
1. Well, almost nothing. Unreachable commits are going to eventually be deleted if enough time passes. By default, they will be kept around for 30 days but you can change that limit through the gc.reflogExpireUnreachable
option.