Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to recover a deleted remote branch

Tags:

git

github

Our remote master branch was deleted. I have a local copy of the master repo but it is a few revs out of date. I'm able to see the branch in github by plugging the last known commit hash into the URL, but have been unsuccessful at restoring it. I've tried several steps to recover it:

git reset --hard 16deddc05cb53dfaa2d198b1cf264416e19255e9
fatal: Could not parse object '16deddc05cb53dfaa2d198b1cf264416e19255e9'

git checkout 16deddc05cb53dfaa2d198b1cf264416e19255e9
fatal: reference is not a tree: 16deddc05cb53dfaa2d198b1cf264416e19255e9

Understandable since master no longer exists. What are my options to recover here?

like image 648
Fook Avatar asked Dec 11 '22 20:12

Fook


2 Answers

  1. Fetch the exact commit (and everything down its line of history):

    git fetch origin 16deddc05cb53dfaa2d198b1cf264416e19255e9
    
  2. Create a branch out of it:

    git branch xyzzy FETCH_HEAD
    

You can combine this into a single step:

git fetch 16deddc05cb53dfaa2d198b1cf264416e19255e9:refs/heads/xyzzy
like image 84
kostix Avatar answered Dec 26 '22 07:12

kostix


If you don't know the hash for the latest rev, you might be out of luck for recovering it. Perhaps the best you can do is simply push the master branch that you have back up to github. Since the revisions are already in the repository, it will be a quick network operation.

If you have ssh access to the machine hosting your repository (which you do not, on github) then you can do a search for orphans in the git repository. An orphan is a commit that no longer has references. Unfortunately, that won't help you in this case.

There are some pointers that can help you recover lost commits, including the process for finding orphans in this post:

Git: Recover deleted (remote) branch

like image 42
mkrufky Avatar answered Dec 26 '22 06:12

mkrufky