1

I deleted a git branch from my server with the command

git push origin :feature-my-branch

I did not have the branch checked out/saved locally. Is there some way I could recover this branch or is it gone forever?

When I do

git branch --all

I cannot see feature-my-branch. When I do

git reflog --no-abbrev 

I can not find the tip of feature-my-branch.

LukesDiner
  • 145
  • 8
  • 2
    I guess it might depend on the remote hosting service. Do you know the hash of the commit the branch referenced? – Lasse V. Karlsen Jul 01 '21 at 12:40
  • 1
    What hosting service do you use? If you have access to the server machine, you could log in to the server and try `git fsck --dangling` in the server repository. It prints the dangling objects. The tip commit may be among them. The tip could also be referred to by other refs, so you could try `git for-each-ref` to list all the refs and check if any of their heads or ancestors is the very commit. – ElpieKay Jul 01 '21 at 12:43
  • Did you fetch before deleting it? – Alex028502 Jul 01 '21 at 14:46
  • 1
    @Alex028502 we know he didn't based on his `git branch --all` and `git reflog --no-abbrev ` result. – Inigo Jul 02 '21 at 00:25
  • If you don't have `feature-my-branch` locally, how did it get onto the server? Did you delete your local copy as well? Or was it pushed from a third repo? If the latter, you can look into recovering it from there. – Inigo Jul 02 '21 at 00:27
  • Does this answer your question? [How to recover a deleted remote branch](https://stackoverflow.com/questions/33656729/how-to-recover-a-deleted-remote-branch) – Inigo Jul 02 '21 at 00:37
  • also: https://stackoverflow.com/questions/45089238/any-way-to-recover-deleted-github-remote-branch-that-was-deleted-locally-also – Inigo Jul 02 '21 at 00:37
  • @Inigo I deleted the branch on the server with `push origin :feature-my-branch` without having a local copy of the branch. – LukesDiner Jul 02 '21 at 07:48
  • @Inigo The answers provided by the links you sent were not helpful for this problem. – LukesDiner Jul 02 '21 at 07:52
  • 1
    I just tried it with github, and even when I clone the whole repo again the hash isn't there. I thought origin would keep the hash after deleting the branch, and then it would all come with the clone. That's really too bad that it lets you delete something you can't see – Alex028502 Jul 02 '21 at 08:38
  • @Inigo clearly the answers does not tell the whole truth since I managed to recover the lost branch without any interaction with other machines? – LukesDiner Jul 07 '21 at 13:16

1 Answers1

0

I managed to recover my branch by doing

git fsck --dangling

where I found a dangling commit which was the head of feature-my-branch. Then I recovered feature-my-branch and pushed it to the server:

git branch feature-my-branch <commit-id>
git push origin feature-my-branch
LukesDiner
  • 145
  • 8