2

Suppose we have just lost a remote repository, because someone accidentally typed the following command.

rm -rf OurRepo.git

In order to restore it, we plan to use one of the top-voted answers to an earlier question.

However, I observed that none of the solutions mentions the following three-step strategy.

  1. Use git init --bare to recreate the remote repository.
  2. Set the new remote git remote add origin <new repository URL>.
  3. Use git push origin master from one of the machines that has an up-to-date copy of the repo

I have tried this on a toy respository and it appears to work, but one of my colleagues claims there is a problem with it without being able to pinpoint why.

Can anyone either confirm or deny whether this is a reasonable way to restore a lost remote?

Community
  • 1
  • 1
merlin2011
  • 71,677
  • 44
  • 195
  • 329
  • 2
    The end result would be the remote repo having the same objects as yours. Should work just fine. – Lucas Trzesniewski Jun 26 '14 at 00:20
  • @LucasTrzesniewski, Would it have any negative (especially unexpected) side effects though, compared to one of the two answers I linked? – merlin2011 Jun 26 '14 at 00:20
  • I can't think of any off the top of my head. But should there be any side effects, they'd be recoverable easily with a `git config`. The important part, the objects, will contain exactly the same data. – Lucas Trzesniewski Jun 26 '14 at 00:24
  • You didn't link to any answers, only the question. Also, your proposed strategy sounds just fine, though don't forget to push any tags with `git push origin --tags` as well. Your coworker says there's a problem but won't say exactly what that problem is? –  Jun 26 '14 at 00:33
  • @Cupcake, He does not remember. I mentioned the top voted answers on the question. – merlin2011 Jun 26 '14 at 00:36
  • The linked question is different to your stated requirement. – M.M Jun 26 '14 at 03:55

1 Answers1

4

No, there shouldn't be anything wrong with your proposed strategy, and in fact this would be a typical way to restore a remote repository (though not the only way).

Don't forget to push your tags along with your branches:

git push origin --tags --all

That command will push all branches under .git/refs/heads/ and all tags under .git/refs/tags/. If you don't want to push all of your branches, just name each branch you want to push instead of using --all.

You'll probably want to avoid using --mirror in this case, because that will push all references under .git/refs/, which will include remote-tracking branches in your local repo, which you probably don't need or want on your remote.

Alternatives

The other alternative to using git push to restore a remote would just be to clone a new remote from another repo with the --bare option:

git clone --bare <otherRepo> <newRepoName>

Documentation