Undeleting branches on Github

This is a short post, partly so I can remember in the future and partly to also help others as silly as I was.

The Situation

I have this repo on Github. I was adding a fairly large feature on a branch, which I typically do as I commute on the way home on the train. Often I lose connectivity so do not get a chance to push changes all the time.

I had been working on this feature sporadically for at least 2 months (scope creep played a big part here). I had pretty much completed it and merged it into master (or so I had believed).

Then I had to go overseas for business, and broke my laptop in the process, requiring that I get a new one (and subsequent scrubbing of data).

I come back from overseas, get a new laptop, I clone my repo, I continue working. I notice that the branch is still there, I delete it.

That was maybe a week or two ago.

As I prepare that repo for its glorious v1 release, I look through some of the code and realise a whole heap of changes are not there. In fact, it is like the branch I worked on was not merged. Probably because it wasn't. At least it wasn't pushed after I merged it. However, I believed I had merged and pushed (yes, I had a senior moment) so I deleted the branch. Felt pretty crappy I have to say after realising I could lose all that work.

I sent a support request to Github asking if they could restore it, but expecting they had better things to do with their time than entertain my stupidity so may not get it restored.

Back from the dark abyss

Turns out you can get deleted branches back, at least in my case as I had pushed that branch prior.

Here is how you can do it (props to this blog here for the massive assist).

  1. Get the SHA of the last commit you made to that branch.
    You can do this using the Github events API. For me, I used Postman and went searching for the last commit. I found it at https://api.github.com/repos/glav/CognitiveServicesFluentApi/events?page=2 . I was looking for 'PushEvents' that targetted the branch I was interested in. Note that I did not need to authenticate or provide a token to do this.
  2. The response will contain a SHA you are interested in. Copy it.
    This was in the "payload" element, "commits" -> "sha"
  3. Now you can use the 'references' API to create a references to that commit. For me, I POSTed to the url: https://api.github.com/repos/glav/CognitiveServicesFluentApi/git/refs
    The body needs to contain the 'ref' you want to create and the SHA from which it is based, like:
    {
       "ref": "refs/heads/PersonGroupSupportUndelete",
       "sha": "1ffa4cb786103aa888f59b6155f4d5c35bfb70f9"
    }
  4. You will need to provide an access token for authentication, otherwise you will receive a "Not Found" error response. So, grab a token by going to Github, going to "Settings -> Developer settings" and generate a personal access token.
  5. As part of the header, you will need to provide an "Authorization" header, and the value will be "token {your generated personal access token}"
  6. With that in place, issue the post request and with any luck, you get a "201 created" response and your branch is back.

So thats it. I hope it helps. It certainly helped me. However, I do hope you are not as silly as me, and never really need it.

2 Comments

  • Thank you so much!!!

    You're a savior!

  • Your composite septic tank is one of the most important components of your entire plumbing structure. Septic tanks safely process and manage all of your home's sewage.

    When your septic tank does not work, you must replace or repair it immediately. When your septic tank does not work properly, your entire plumbing system is disrupted and you may endanger yourself and your family and damage your property or backyard.

Add a Comment

As it will appear on the website

Not displayed

Your website