Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to recover deleted git branch?

Tags:

git

By mistake I deleted one of my git branches. Is there any way to recover it?

I used the following command:

$ git push :development

I want to recover this branch. I am on the master branch and it doesn't show any delete command is run:

$ git reflog

1b716a1 HEAD@{0}: checkout: moving from master to origin
1b716a1 HEAD@{1}: reset: moving to origin
91791dc HEAD@{2}: reset: moving to 91791dc
1b716a1 HEAD@{3}: checkout: moving from master to master
1b716a1 HEAD@{4}: pull: Merge made by the 'recursive' strategy.
91791dc HEAD@{5}: commit: Fix Total Label crash
198de6f HEAD@{6}: commit: Fix the Total Label crash
like image 619
GhostRider Avatar asked Dec 27 '22 17:12

GhostRider


1 Answers

Do you have another (recent enough) clone of the repository? Or does somebody else have a clone you can get access to? (Maybe someone forked it on github). If so, you can push the branch from the other repository and everything should be fine.

If you don't, things get a little bit more complicated. First, if you worked on the development branch recently, it must appear in the HEAD reflog – unless you have expired the reflog manually (the default is to expire reflog entries after 30 days).

If you never worked locally on the branch (IOW: no checkout or commit), there's one last chance you get it back: use git fsck --unreachable --lost-found and then inspect all reported commit objects. When you have found the correct one (the old tip), then issue git branch development <hash of the commit>. After re-creating the branch locally, you can push it to GitHub again: git push origin development:development.

Good luck!

like image 92
knittl Avatar answered Jan 08 '23 00:01

knittl