我有两个 git 存储库:
- report.git(远程位置的主控)
- cloned.git(本地)
我丢失了report.git。我有克隆的.git。我想从这个 cloned.git 克隆其他存储库。这是可能的,但我的问题是我错过了什么?cloned.git真的和主report.git一样吗?
cloned.git 仍然指向主报告.git。我通过删除 .git/config 中的选项来改变这一点。这够了吗?
您的 cloned.git 存储库是 report.git 的克隆(副本),处于您克隆或上次从 report.git 中提取时 report.git 的状态
您的 cloned.git 一直是大师,report.git 也一直是大师。这是 git 的美妙之处。
git clone --bare localrepo.git newremoterepo.git
注意:仅当“克隆”存储库是工作存储库(即您提交的非裸存储库,使用单独的远程布局)而不是裸(或镜像)存储库时,才需要下面描述的复杂技术。如果 'clone.git' 是一个裸存储库,那么git clone --bare
(或git clone --mirror
)恢复原始的 'report.git' 存储库就足够了(就像alinrus 写的那样)。
假设您使用默认配置和现代 Git,这意味着“单独的遥控器”布局,您将在 'remotes/origin'(或 'remotes/report')命名空间中拥有 'report.git' 的远程跟踪分支。
要重新创建“report.git”存储库,您必须从远程跟踪分支推送(或获取)一次到普通分支,恢复普通获取 refspec(至少对于分支:标签总是镜像)。因此,一次推送(或获取)的 refspec 将类似于以下内容:refs/remotes/origin/*:refs/heads/*
plus refs/tags/*:refs/tags/*
.
让我们假设原始的“report.git”存储库包含两个分支:“master”和“next”,并且没有标签(标签是 1-1 映射的,所以无论如何它们应该不是问题):
$ git init --bare report.git
# ...
[report.git] $ git branch -a
* master
next
让我们将“克隆/.git”存储库作为“report.git”的普通(非裸和非镜像)克隆:
$ git clone user@example.com:report.git cloned
Initialized empty Git repository in /tmp/jnareb/cloned/.git/
# ...
[cloned] $ git branch -a
* master
remotes/origin/HEAD -> origin/master
remotes/origin/master
remotes/origin/next
[cloned] $ git remote show origin
* remote origin
Fetch URL: user@example.com:report.git
Push URL: user@example.com:report.git
HEAD branch: master
Remote branches:
master tracked
next tracked
Local branch configured for 'git pull':
master merges with remote master
Local ref configured for 'git push':
master pushes to master (up to date)
当然,您的分支机构的状态以及当地分支机构的数量可能会因您的情况而有所不同。这只是一个简单的例子。
现在让我们删除,或者更好地重命名(移到一边)原始存储库“report.git”,以测试我们的恢复操作
$ mv report.git report-copy.git
现在我们重新获取在 'clone/.git' 中最后一次 fetch/pull 操作时的 'report.git' 状态:
$ git init report.git # push won't create new repository
# move to 'cloned' repository
[cloned] $ git push user@example.com:report.git 'refs/remotes/origin/*:refs/heads/*'
Counting objects: 12, done.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (12/12), 839 bytes, done.
Total 12 (delta 1), reused 0 (delta 0)
Unpacking objects: 100% (12/12), done.
warning: updating the current branch
# long warning about pushing into current branch
To user@example.com:report.git
* [new branch] origin/HEAD -> HEAD
* [new branch] origin/master -> master
* [new branch] origin/next -> next
现在您可以比较 'report.git' 和 'report-copy.git' 是否相同。如果 'report.git'是非裸的,它们可能会有所不同,因为推送不会更新工作目录(你需要自己做git reset --hard HEAD
或git checkout
在 'report.git' 中),但它是一个裸存储库,不是吗?
HTH(希望有帮助)。