Restoring a delete git remote branch

I'm a spaz. Last night I've felt like tidying up, so I've started dropping my branches in remote and locally. Few hours later I've realised some pull requests are gone. Here's how I've saved the day.

The local branch is gone, the remote branch is also gone. What do i do now?

Good solution would be to search for SHA that's printed every time you delete a branch.

With that I could have git checkout -b new-branch-name SHA and I'd be fine. Too bad I've deleted 30-40 branches through a shell script and have already closed the terminal window (told you, i'ma spaz).

Next suggestion would be git reflog but that didn't do it for me either. Previously "local history" plugins in my IDE saved me in situations like that, yet i had none in vscode so I'll cut straight to the chase, here's what I did:

  1. ran git fsck --lost-found - this would print all the lost boys. Out of these i suggest you filter out ones that start with "dangling commit"

  2. Now you have hundreds of commit hashes without a message that you can theoretically look into. Still bad? - Don't lose hope. What i did next was: i put all the commits into a big space-separated line. Very long line. And ran a shell script


for i in [those 200 items] do

   git show $i

  1. I've re-directed output of this script into a file with > pipe, cmd+f in that huge text file and found diffs of my missing commits by names of classes and functions.

  2. I've cherry-picked them by SHA into a new branch

Perhaps there's a better easier way, grep masters surely know, but here's what i did. Maybe it helps you too, you silly Milly.

Enjoy yo day boys and girls.

25-05-2023, work i guess