4

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
Mat
  • 202,337
  • 40
  • 393
  • 406
GhostRider
  • 1,197
  • 10
  • 19
  • @GlostRider .. Check this [link](http://stackoverflow.com/questions/4674226/does-github-keep-deleted-remote-branches-in-history-if-so-can-those-be-restore) – thar45 Sep 15 '12 at 12:33
  • Do you still have a local branch pointing to the tip of the deleted branch? – knittl Sep 15 '12 at 12:55
  • @Stranger: I read the link but its tell to create new branch – GhostRider Sep 15 '12 at 13:10
  • Well, you have to re-create the branch somehow. For that you need to know the hash of its last commit – knittl Sep 15 '12 at 13:49

2 Answers2

9

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!

knittl
  • 246,190
  • 53
  • 318
  • 364
4

This is an old question, but maybe it will benefit someone else.

You're right to do git reflog, and you're right that there is nothing to help you identify branches were deleted.. but that's okay.

Let's assume the branch you deleted was titled special.

In a hypothetical situation, let's assume you were on master, checked out a new branch special, made some changes, committed them, switched back to master, and then deleted special accidentally, perhaps via some command like git branch -D special.

Run your git reflog command, and you'll see an output like this.

ef15850 HEAD@{411}: checkout: moving from special to master
64e7b02 HEAD@{412}: commit: update special with stuff
b444040 HEAD@{413}: checkout: moving from master to special

You can search for the branch name you deleted. You will find it somewhere.

Since you can't delete a branch that you were active on, you must have left that branch at some point in order to delete it.

You can go to the commit hash immediately prior to when you switched branches and create a new branch based off your deleted one.

In this case, git checkout -b recovered_special 64e7b02

Trevor
  • 1,284
  • 3
  • 15
  • 33