The reflogs
Okay, but what if we ignore the Git message the first time, then time goes and at the end, we can't remember the hash of the commit we want to retrieve?
Git never forgets you. It has another powerful tool in its wrench box, and that is called the reference log, or reflog for short. Basically, the reflog (or better the reflogs, as there is one for every reference) records what happens in the repository while you commit, reset, check out, and so on. To be more precise, every reflog records all the times that tips of the branches and other references (such as HEAD) where updated.
We can take a look at it with a convenient Git command, git reflog show:
[53] ~/grocery (master) $ git reflog show 0e8b5cf HEAD@{0}: checkout: moving from 07b18581801f9c2c08c25cad3b43aeee7420ffdd to master 07b1858 HEAD@{1}: commit: Bug eats all the fruits! e4a5e7b HEAD@{2}: checkout: moving from master to HEAD^ 0e8b5cf HEAD@{3}: reset: moving to 0e8b5cf e4a5e7b HEAD@{4}: reset: moving to HEAD^ 0e8b5cf HEAD@{5}: checkout: moving from melons to master a8c6219 HEAD@{6}: checkout: moving from master to melons 0e8b5cf HEAD@{7}: checkout: moving from berries to master ef6c382 HEAD@{8}: reset: moving to HEAD^ a8c6219 HEAD@{9}: commit: Add a watermelon ef6c382 HEAD@{10}: reset: moving to ef6c382 ef6c382 HEAD@{11}: reset: moving to ef6c382 0e8b5cf HEAD@{12}: reset: moving to master ef6c382 HEAD@{13}: checkout: moving from master to berries 0e8b5cf HEAD@{14}: checkout: moving from berries to master ef6c382 HEAD@{15}: commit: Add a blackberry 0e8b5cf HEAD@{16}: checkout: moving from master to berries 0e8b5cf HEAD@{17}: commit: Add an orange e4a5e7b HEAD@{18}: commit: Add an apple a57d783 HEAD@{19}: commit (amend): Add a banana to the shopping list c7a0883 HEAD@{20}: commit (initial): Add a banana to the shopping list
Actually, here there are all the movements the HEAD reference made in my repository since the beginning, in reverse order, as you may have already noticed.
In fact, the last one (HEAD@{0}) says:
checkout: moving from 07b18581801f9c2c08c25cad3b43aeee7420ffdd to master
Actually, this is the very last thing we did, apart from the creation on the bug branch. As we never moved into it, the HEAD reflog doesn't log anything about bug branch creation.
The reflog is a quite complex topic to be discussed in depth, so here we only learn how to open and read it, and how to interpret information from it.
The only things I want you to know are that this log will be cleared at some point; the default retention is 90 days. Then, there is a reflog for every reference; what we are seeing now is the HEAD reflog (HEAD@ is a hint about this), but if you type git reflog show berries you will see the movements berries branch did in the past:
[54] ~/grocery (master)
$ git reflog berries
ef6c382 berries@{0}: reset: moving to HEAD^ a8c6219 berries@{1}: commit: Add a watermelon ef6c382 berries@{2}: reset: moving to ef6c382 0e8b5cf berries@{3}: reset: moving to master ef6c382 berries@{4}: commit: Add a blackberry 0e8b5cf berries@{5}: branch: Created from master
To go back to our problem, if we want to check out a currently unreachable commit, we can go to the HEAD reflog and look for a line where we did the commit (in this example, I would look for a commit: logline, searching the one where the commit message says something that helps me to remind, something like bug in this case).
Well done, that's enough for now; later we will use reflog again.