Fook
Fook

Reputation: 5550

How to recover a deleted remote branch

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?

Upvotes: 5

Views: 13695

Answers (3)

kostix
kostix

Reputation: 55443

  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

Upvotes: 9

mkrufky
mkrufky

Reputation: 3388

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

Upvotes: 5

mkrufky
mkrufky

Reputation: 3388

If you do know the hash of the latest missing commit, try to resolve the problem using github's Web UI. Go to the following URL:

https://github.com/{username}/{repository}/compare/{hash}

A button should appear, Create pull request -- use this to create a pull request and merge your history back into a branch.

Upvotes: 2

Related Questions