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:

Screen shot 2011 02 23 at 10 37 00 AM

to this:

Screen shot 2011 02 23 at 10 37 00 AM copy

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

Screen shot 2011 02 23 at 11 31 03 AM

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

Screen shot 2011 02 23 at 11 44 02 AM

And like magic, the bioreference branch exists and points to the correct commit, including all of the commit ancestry.

Screen shot 2011 02 23 at 10 37 00 AM

Git: Oops! I didn’t mean to commit on that remote tracking branch!