4

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?

Fook
  • 5,320
  • 7
  • 35
  • 57
  • Do you really have this object locally? What does `git cat-file -t 16deddc05cb53dfaa2d198b1cf264416e19255e9` tell? – kostix Nov 11 '15 at 18:18

3 Answers3

8
  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
kostix
  • 51,517
  • 14
  • 93
  • 176
  • The fetch fails with "error: no such remote ref 16deddc05cb53dfaa2d198b1cf264416e19255e9" even though I can see it in the web UI. – Fook Nov 11 '15 at 18:56
  • 1
    @Fook, I've verified it works with one of my repos (not hosted on Github). Hence I think what you observe is peculiar to Github (IIUC they don't use vanilla Git anyway), and you should try Michael's suggestion. – kostix Nov 12 '15 at 07:29
  • 2
    Awesome! This worked flawlessly for a remote branch I had accidentally deleted. Thanks. – Dário Dec 15 '17 at 14:30
  • Perhaps a bit late for you, but I wrote [a small script](https://github.com/cryslith/git-recover-commit) which will create a remote branch at the desired commit, even though you don't have the commit locally. – Lily Chung Nov 07 '19 at 07:01
5

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

Community
  • 1
  • 1
mkrufky
  • 3,268
  • 2
  • 17
  • 37
  • I do have the latest hash. – Fook Nov 11 '15 at 18:02
  • then you can use github's web UI to compare that hash against your master branch and create (and merge) a pull request. I added another answer explaining how to do that using the GitHub web UI. – mkrufky Nov 11 '15 at 18:04
2

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.

mkrufky
  • 3,268
  • 2
  • 17
  • 37
  • Tried that but no button appeared. It shows the diff and lets me comment on the commit, but no way to create a pull request. – Fook Nov 11 '15 at 18:58
  • Can you share the github link? Maybe I can figure out why it doesn't offer the `Create pull request` button. – mkrufky Nov 11 '15 at 19:01
  • It's an enterprise github installation behind a VPN, unfortunately. – Fook Nov 11 '15 at 19:43