Tuesday, April 20, 2010

Recover a deleted branch from a Git repository

So I thought I would quickly note this since I managed to reach into a Git repo and pull out code that I thought I had foolishly deleted. I was in a rush during a release two weeks ago and in a fit to clean up the repo I was working in trashed a feature branch for code I was working on.

My colleague Evan said there was a way to grab the branch since it's still in the .git objects (since I had not done a ``git-gc``). But the method for that was left as an exercise for me, the reader.

I used the command suggested by Iamamac on Stack Overflow.

So here's what I did:

$> git fsck --full --no-reflogs | grep commit

This finds the "dangling commits" in the repository. Again, the commit for the HEAD of the branch is still there - but it's just not listed as a ref/head. This will get you a list like so:

dangling commit 0c91cb293..
dangling commit 1896a7b7f..
dangling commit ec9ac3cd..
dangling commit 5c6a8f1af..
dangling commit fd729386..

Then just use ``git-log`` w/ some extra verbosity to find the HEAD commit you're looking for:

$> git log --stat 5c6a8f1af

Which gives you output like:

commit 5c6a8f1af
Author: lenards
Date: Wed Mar 24 08:56:20 2010 -0700

Test new version of gwtupload

pom.xml | 2 +-
1 files changed, 1 insertions(+), 1 deletions(-)

I hope that helps someone out...

(and thanks to Scott Chacon and everyone in the Git community for sharing tutorials, advice, and wisdom)

12 comments:

NicolaSigel0508 said...

恐佈哦~~
相愛?相害?
看了鬼故事之後讓你睡不著覺
洗臉的女孩
紅燒人頭,可好吃了
紅布床簾

o0625TaylorJ_Duraz said...

失去金錢的人,失去很多;失去朋友的人,失去更多;失去信心的人,失去所有。 ..................................................

SungR_Auclair0佳亦 said...

Thx ur share........................................

BenWann1259 said...

thanks................................................

劉KarolR_Sundquis said...

大家隨便說,說什麽都好,就是請不要惦惦.............................................

ErinBurlingam雅萍 said...

Efforts to fight fuel!........................................................

韋于倫成 said...

在莫非定律中有項笨蛋定律:「一個組織中的笨蛋,恆大於等於三分之二。」......................................................

ErnestoE_S陽峰ommers0909 said...

幸福不是一切,人還有責任。..................................................

宛美黃惠妹 said...
This comment has been removed by a blog administrator.
SadeRa盈君iford0412 said...

Make hay while the sun shines.............................................................

Vi2139 said...

恨一個人,比原諒一個人,更傷力氣。..................................................................

皇銘 said...

你不能左右天氣,但你可以改變心情.............................................................