首页 > 解决方案 > 克隆一个镜像的本地 Git 存储库

问题描述

我已经镜像 ( clone --mirror) 一个没有远程引用的本地存储库,仅用于备份目的。我注意到备份的 repo 有一个带有本地refs 的“packed-refs”文件。

当我尝试将其克隆回来时,我得到了相同的“打包引用”文件,其中包含远程引用,例如remotes/origin/heads/...而不是本地引用。然后,我必须检查每个 refs 以获得本地 refs。

有没有办法,也许是一些clone选项,能够直接恢复本地参考?

标签: gitclonemirror

解决方案


如果您从某个现有克隆中创建了一个裸克隆,并且现有克隆已被销毁(或者您可能正在新的服务器设置上重新创建它),您有两种选择:

  • 将新的裸克隆创建为空克隆,然后使用git push --mirror 备份/镜像克隆新克隆,或者
  • 从现有的裸克隆中使用git clone --mirror来创建一个新的裸克隆,然后可以选择删除在新的裸克隆中创建的远程。

例如,假设ec$这里的提示是在现有的镜像克隆上ec.example.com,并且ns$是新服务器上的提示,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

git push --mirror中设置新服务器的克隆/git/restored

或者:

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

git remote remove步骤是可选的;它清理了表明恢复的裸镜像克隆来自的痕迹ec.example.com/~user/backup/the-mirror。由于这些跟踪仅对首先实际登录到服务器的任何人可见,因此这并不是真正需要的,但您可能出于自己的任何原因喜欢它。


推荐阅读