Craig Walker Craig Walker - 10 months ago 70
Git Question

Git: Recover deleted (remote) branch

I need to recover two Git branches that I somehow deleted during a push.

These two branches were created on a different system and then pushed to my "shared" (github) repository.

On my system, I (apparently) retrieved the branches during a fetch:

~/myfolder> git fetch
remote: Counting objects: 105, done.
remote: Compressing objects: 100% (58/58), done.
remote: Total 62 (delta 29), reused 0 (delta 0)
Unpacking objects: 100% (62/62), done.
* [new branch] contact_page -> origin/contact_page
731d1bb..e8b68cc homepage -> origin/homepage
* [new branch] new_pictures -> origin/new_pictures

Right after that I did a push to send my local changes up to the central repo. For some reason, these branches were deleted from both my local system and the central repo:

~/myfolder> git push
Counting objects: 71, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (43/43), done.
Writing objects: 100% (49/49), 4.99 KiB, done.
Total 49 (delta 33), reused 0 (delta 0)
- [deleted] contact_page
+ e8b68cc...731d1bb homepage -> homepage (forced update)
bb7e9f2..e0d061c master -> master
- [deleted] new_pictures
e38ac2e..bb7e9f2 origin/HEAD -> origin/HEAD
731d1bb..e8b68cc origin/homepage -> origin/homepage
e38ac2e..bb7e9f2 origin/master -> origin/master
* [new branch] origin/contact_page -> origin/contact_page
* [new branch] origin/new_pictures -> origin/new_pictures

It's not terribly easy to get the branches off of their birthplace machine, so I'd like to try and recover them from my local if possible.

All of the git "undo" information I've googled has to with recovering lost commits. I don't think that applies here, since I don't have commit UIDs for these branches.

I'd like to know how I can get these back. I'd also like to know how they were deleted in the first place and how I can avoid this in the future.

EDIT: by request, here's my repo configuration Walker
alias.unadd=reset HEAD
alias.undo=reset --hard
alias.test=push -f ci HEAD:master
alias.lg=log -p
alias.who=shortlog -s --*:refs/remotes/ci/**:refs/remotes/cynthia/*

Answer Source

I'm not an expert. But you can try

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

to find the HEAD commit of deleted branch and get them back.