Sunday, April 11, 2010

Recover deleted branches in Git

Accidentally deleted a branch? No problem. Git reflog to the rescue.

git checkout -b recover_lost_commits

git reflog

e9c17db... HEAD@{0}: checkout: moving from stable to master
e9c17db... HEAD@{1}: checkout: moving from stable to e9c17dbfffd79f2bf30530e144c43c0f47322826^0
318c0f2... HEAD@{2}: checkout: moving from master to stable
e9c17db... HEAD@{3}: commit: Forgot to add images to CSS and adjusted positions.
3baab51... HEAD@{4}: commit: Added new buttons.

git merge e9c17db


Adventsparky said...

Absolutely perfect, so simple and compact. Thanks for this!

humpy said...

Thank you.

Giannis Dze said...

I had a mini heart attack when I realized I deleted the wrong branch!

Thanks a lot!