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

About Derick Bailey

Derick Bailey is an entrepreneur, problem solver (and creator? :P ), software developer, screecaster, writer, blogger, speaker and technology leader in central Texas (north of Austin). He runs - the amazingly awesome podcast audio hosting service that everyone should be using, and where he throws down the JavaScript gauntlets to get you up to speed. He has been a professional software developer since the late 90's, and has been writing code since the late 80's. Find me on twitter: @derickbailey, @mutedsolutions, @backbonejsclass Find me on the web: SignalLeaf, WatchMeCode, Kendo UI blog, MarionetteJS, My Github profile, On Google+.
This entry was posted in Git. Bookmark the permalink. Follow any comments here with the RSS feed for this post.