Search code examples
gitbranchrecoverydata-recovery

Can I recover a deleted git branch which I did not save locally?


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.


Solution

  • 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