I was 开发者_JAVA百科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.
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>
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?"
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.
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.
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.
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.
精彩评论