DEV Community

Roman Guivan
Roman Guivan

Posted on

How I've deleted my git remote branch and kind of managed to restore it

Restoring things (branches) from git that were deleted in git, git git git git.

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.

So my local branch is gone, my 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 branch. With that you can git checkout -b new-branch-name SHA and you'll be fine. But not me, i've deleted 30-40 branches through a shell script (told you, i'ma spaz).

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

  1. you run 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" (that's what i did)

  2. Now you have hundreds, maybe thousands 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 commits into a biiiig space-separated line. Very long line. And ran a shell script

#!/bin/bash

for i in [those 200 items ]do

Β Β  git show $i

done
Enter fullscreen mode Exit fullscreen mode
  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

Top comments (0)