Search code examples
gitclonemirror

Cloning back a mirrored local Git repository


I've mirrored (clone --mirror) a local repository with no remote refs, for a backup purpose only. I notice that the back'd up repo have a 'packed-refs' file with local refs inside.

As I try to clone it back I get the same 'packed-refs' file with remote refs such as remotes/origin/heads/... instead of local ones. Then, I've to checkout every refs in order to get the local refs.

Is there a way, maybe some clone options, able to restore the local refs directly?


Solution

  • If you've made a bare clone from some existing clone, and the existing clone has been destroyed (or perhaps you're re-creating it on a new server setup), you have two options:

    • create the new bare clone as an empty clone, then use git push --mirror from the backup/mirror clone to the new clone, or
    • use git clone --mirror from the existing bare clone to make a new bare clone, then optionally remove the remote that this created in the new bare clone.

    For example, let's say that the prompt ec$ here is on the existing mirror clone on ec.example.com, and ns$ is the prompt on the new server, ns.example.com:

    ns$ cd /git && mkdir restored && cd restored && git init --bare
    [git messages happen here]
    
    ec$ git push --mirror ssh://ns.example.com/git/restored
    

    The git push --mirror sets up the new server's clone in /git/restored.

    Or:

    ns$ cd /git
    ns$ git clone --mirror ssh://ec.example.com/~user/backup/the-mirror restored
    [git messages happen here]
    
    ns$ cd restored
    ns$ git remote remove origin
    

    The git remote remove step is optional; it cleans up the traces that show that the restored bare mirror clone came from ec.example.com/~user/backup/the-mirror. Since those traces are only visible to anyone who actually logs on to the server in the first place, this isn't really required, but you might like it for whatever reasons of your own.