Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 939
luisgo Avatar asked Jan 12 '11 21:01

luisgo


People also ask

How long does GitHub keep deleted branches?

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.

Does deleting git branch delete history?

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.

What happens when you delete a remote branch in git?

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 .

Is it possible to recover deleted git branch?

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.


2 Answers

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> 
like image 124
Highway of Life Avatar answered Sep 16 '22 20:09

Highway of Life


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

like image 29
VonC Avatar answered Sep 19 '22 20:09

VonC