luisgo
luisgo

Reputation: 2465

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

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.

Upvotes: 76

Views: 108470

Answers (7)

RiverHeart
RiverHeart

Reputation: 872

Yes but the interface is borderline hostile to get to it.

Starting from the repo page:

  • Select "Branches" next to the branch dropdown.
  • Select the 3 dots for "main" (default branch)
  • Select "Activity" from dropdown
  • Switch branch filter from "Main" to "View Activity for all Branches"
  • Switch activity filter from "All Activity" to "Branch deletion"
  • Select the 3 dots for a given branch
  • Select "Restore Branch"

Upvotes: 11

VonC
VonC

Reputation: 1326636

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?"

Upvotes: 17

piersb
piersb

Reputation: 596

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.

Upvotes: 6

Gordon Bean
Gordon Bean

Reputation: 4612

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.

Upvotes: 11

Highway of Life
Highway of Life

Reputation: 24411

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>

Upvotes: 83

Jim Zucker
Jim Zucker

Reputation: 870

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.

Upvotes: 1

Alexander Bird
Alexander Bird

Reputation: 40659

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.

Upvotes: 1

Related Questions