Skip to content

Instantly share code, notes, and snippets.

@alexjyong
Last active October 25, 2023 19:30
Show Gist options
  • Save alexjyong/63172c9113e9c8cf572a1fc97cfeffb4 to your computer and use it in GitHub Desktop.
Save alexjyong/63172c9113e9c8cf572a1fc97cfeffb4 to your computer and use it in GitHub Desktop.
Restore a deleted branch
  1. Fire up a codespace instance in the repo
  2. Run gh api repos/OWNER/REPO/events > myJson.json
  3. The JSON that comes out is unreadable, so clean it up with jq . myJson.json > clean.json
  4. Go through the json file, find the DeleteEvent where you nuked your branch, and below it, find the last push event you did to your branch. In the payload, find the head's SHA and grab it.
  5. Create a JSON file in this format
{
  "ref": "refs/heads/YOUR_BRANCH_NAME",
  "sha": "SHA_VALUE"
}
  1. Save the file then run this gh command gh api https://api.github.com/repos/OWNER/REPO/git/refs --input <json_file_you_just_made>.json

This will recreate your branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment