How to recover deleted git branch?

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

I used the following command:

$ git push :development 

I want to restore this thread. I am on the main branch and it does not show that the delete command is running:

 $ 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 
+4
source share
2 answers

Do you have another (fairly recent) repository clone? Or does anyone else have a clone that you can access? (Maybe someone forked it on github). If so, you can push a branch from another repository, and everything should be fine.

If you do not, it will become a little more complicated. Firstly, if you have recently worked with the development branch, it should appear in the HEAD reflog - if you did not expire the reflog manually (by default, the reflog expires after 30 days).

If you have never worked locally on a branch (IOW: no check or commit), there is another chance to get it back: use git fsck --unreachable --lost-found , and then check all reported commit objects. When you find the right one (old tip), type git branch development <hash of the commit> . After re-creating the branch locally, you can click it again on GitHub: git push origin development:development .

Good luck

+8
source

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

You are right to do a git reflog , and you are right that there is nothing that could help you identify branches that have been deleted .. but that’s good.

Suppose the remote branch was named special .

In a hypothetical situation, suppose you were on master , checked the new special branch, made some changes, committed them, returned to master , and then deleted special accident, possibly through some command like git branch -D special .

Run the git reflog and you will see this output.

 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 find the name of the branch that you deleted. You will find it somewhere.

Since you cannot delete the branch in which you were active, you must leave this branch at some point in order to delete it.

You can go to the commit hash just before switching branches and create a new branch based on the remote.

In this case git checkout -b recovered_special 64e7b02

+1
source

Source: https://habr.com/ru/post/1434357/


All Articles