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.
I asked GitHub Support, this was their response (emphasis mine): We use a separate ref namespace for all Pull Requests which we use for various things including restoring the branch. Since we keep those [Pull Request] refs indefinitely, there's no time limit on restoring a branch.
The commits will still be retained in the repository and it is possible to recover them immediately after the delete, but eventually they will be garbage collected. Thanks for the answer.
Pushing to delete remote branches also removes remote-tracking branches. Note that deleting the remote branch X from the command line using a git push will also remove the local remote-tracking branch origin/X , so it is not necessary to prune the obsolete remote-tracking branch with git fetch --prune or git fetch -p .
A deleted Git branch can be restored at any time, regardless of when it was deleted. Open your repo on the web and select the Branches view. Search for the exact branch name using the Search all branches box in the upper right. Click the link to Search for exact match in deleted branches.
Yes, it's possible to restore a deleted branch from git.
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.
git checkout -b branch_name HEAD@{27}
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?"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With