Sometimes you might make a mistake while resetting the HEAD and erase a commit you did not mean to erase. As an example, look at the Git Bash image. Here I've used a reset hard to 6 commits before the HEAD pointer. To recover everything, we'll use the git reflog command.
To view the reflog, we run
git reflog
If we don't supply any other options, Git will show us how our HEAD pointer has moved throughout the history of our repository. Every entry in the log has a unique identifier, starting from HEAD@{0} (the last position of the pointer) and moving up one each time the pointer moved. Next to the identifier, there's a description of what happened in that entry. The commit ID to the left is the commit to which the pointer was pointing, after the operation in that entry. So, for example, after the last operation (which was a reset) the pointer is at commit af26a96.
In this case, to recover our lost commits, we can reset the HEAD pointer to commit 3e3b5ed (the second entry in our reflog). To do this, we run
git reset --hard 3e3b5ed
We could also use the entry identifier. In that case, we should have used
git reset --hast HEAD@{0}
To see the reflog of other pointers we run
git reflog show pointer_name
and Git will show us its history. Remember that each branch is a pointer in Git, so we could use it for branches. We can also use reflogs with time indications. So, for example
git reflog master@{one.week.ago}
will show us where master was, one week ago.