Git: D’OH! I didn’t want to delete that branch!
In the process of writing up the previous blog post, I accidentally deleted my “bioreference” branch, with no current commit pointing to it. To illustrate, my repository went from this:
to this:
All of my commits appear to be gone… they don’t show up in the commit log, at least.
Enter The Reflog
Did you know there are two different logs in git? There’s the commit log – which is what tools like GitK, GitX and the git log command line show. Then, there’s the git reflog. This is the history of everything that happens to a branch’s HEAD (the pointer that is the branch) and give us the ability to un-delete a branch by letting us know what commit that branch was previously pointing to.
Run git reflog from the command line and it shows you what has been going on with the branch HEADs
Branch From A Starting Point
In this case, we can see near the middle of the screen shot that there is a reference to the “bioreference” HEAD where it is moved to a new commit. All we need to do is take the commit reference (either the hex characters or the treeish “HEAD@{#}”). Once we have that, we can branch with a starting point of the commit in question
And like magic, the bioreference branch exists and points to the correct commit, including all of the commit ancestry.