Does github keep deleted remote branches in history? If so, can those be restored?

GitGithubGit Branch

Git Problem Overview


I was wondering if there is a way to restore a remote deleted branch in github. History clearly keeps record of the branch and merges with other branches but I'm not sure if it's possible to restore a deleted branch.

Thanks.

Git Solutions


Solution 1 - Git

Yes, it's possible to restore a deleted branch from git.

Find your Commit ID: Search for a branch using git reflog

If you had the branch in your local git repo within the last 30 days, you may be able to find it in the reflog using the following:

git reflog

Search for the branch name in the reflog and note the HEAD{x} point or the commit ID.

Re-create the branch from the Reflog HEAD point:

git checkout -b branch_name HEAD@{27}

Re-create the branch from the commit ID:

You can checkout the commit ID and create a branch off of that commit point:

git checkout -b branch_name <commit id>

Solution 2 - Git

It is possible to ask for GitHub support and have them look into the reflog of your remote repo (like in this thread for example).
If this is close enough (less than 30 days per default) from the deletion, the reflog still contains the commits which are no longer referenced by any branch.
Creating a branch on one of those commits allow them to be again accessible.

For more on reflog, see "what the heck is a reflog and why is it so important?"


Update: the repo owner can also query the GitHub EVents API:
See "Does GitHub remember commit IDs?"

Solution 3 - Git

When the branch has been deleted for a very long time (in my case, 1 year), but you had opened a pull request for that branch, you may be able to resurrect it by searching in the pull requests history.

Once I found the pull request for that branch I could restore the branch. Relevant commit information, etc. are also available from the pull request.

Solution 4 - Git

git reflog will show you the history of HEAD. If the branch you deleted was named foo, then in that output, you should see lines like 48534f5 HEAD@{0}: checkout: moving from master to foo or 48534f5 HEAD@{1}: merge foo: Fast-forward. You can search the output of git reflog to figure out which commit must be the latest one that foo pointed to.

Do realize, that the "foo" reflog file itself is deleted when foo was deleted, but since the HEAD's reflog is different it still exists.

Solution 5 - Git

Take a look at this python script for github events. https://github.com/jimzucker/githubutils/blob/master/githubreflog.py

I created it to pull events and make them readable, you can pipe it in to grep and look for the branch you are interested in. if there is enough history you will see the delete event for the branch in question, the next line will be the last push event and that is the sha you are interested in.

Solution 6 - Git

It's a bit of a runaround, but here's how to do it.

Get yourself a new Personal Access Token from Profile / Settings / Developer Settings / Personal Access Tokens if you don't have one already.

curl -u "username:PersonalAccessToken" -H "Accept: application/vnd.github.v3+json"  https://api.github.com/repos/RepoOwner/Repo/events

Find the DeleteEvent in the response; in there you'll be able to find the orphaned SHA of the branch you deleted.

git fetch SHA
git switch -c name-of-your-deleted branch

Problem solved.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionluisgoView Question on Stackoverflow
Solution 1 - GitHighway of LifeView Answer on Stackoverflow
Solution 2 - GitVonCView Answer on Stackoverflow
Solution 3 - GitGordon BeanView Answer on Stackoverflow
Solution 4 - GitAlexander BirdView Answer on Stackoverflow
Solution 5 - GitJim ZuckerView Answer on Stackoverflow
Solution 6 - GitpiersbView Answer on Stackoverflow