Dude, where’s my git repo?

Imagine you’ve joined a startup as the tech lead. The founder has a prototype, which she built with a contractor. Conveniently, the contractor hosted the code on github, so you fork the repository and you’re off and running. Months of building go by. The contractor, while still friendly with everyone, has moved on.

One day, you go to push your latest commits to github, and you get an error message that the remote repository cannot be found. You log into github, and the repository is gone. All traces of it have disappeared.

What happened? It turns out that Github treats private repositories in a special and unexpected way: private forks disappear when their ancestor is deleted from github. So when the contractor, needing to free up space in his account, removed the prototype repository, all its children went with it. Caveat emptor.

This might sound scary, but since every git repository carries its history, it’s actually pretty straightforward to recover so long as you have an up to date clone of the repo handy. Here is the procedure:

  1. On github, create an empty private git repository having the same name as the old one, in the same github account as your fork.
  2. On a machine containing a checkout of the code, make a bare clone of the repo and mirror it to the re-created repo on github:
    git clone --bare /path/to/code /tmp/repo.git
    cd /tmp/repo.git
    git push --mirror git@github.com:username/repo.git
  3. Now you can fetch, pull and push in the original checkout as if nothing happened.