在git中创建克隆的存储库

时间:2009-11-07 22:07:12

标签: git clone git-clone

我有两个git存储库:

  1. report.git(远程位置的主人)
  2. cloned.git(Local)
  3. 我丢失了report.git。我有cloned.git。我想从这个cloned.git克隆其他存储库。这是可能的,但我的问题是我错过了什么? cloned.git和master report.git真的一样吗?

    cloned.git仍然指向Master report.git。我通过删除.git / config中的选项来改变这一点。这够了吗?

3 个答案:

答案 0 :(得分:6)

您的cloned.git存储库是report.git的一个克隆(副本),该状态是您在报告或上次从report.git中提取时的report.git

你的cloned.git一直是大师,report.git也一直是大师。这是git的美丽。

答案 1 :(得分:5)

git clone --bare localrepo.git newremoterepo.git

http://git-scm.com/docs/git-clone

答案 2 :(得分:3)

注意:只有当'克隆'存储库是工作存储库(即您提交的非裸存储库)时才需要下面复杂的技术,使用单独的遥控器布局),而不是裸(或镜像)存储库。如果'cloned.git'是一个裸存储库,那么git clone --bare(或git clone --mirror)就足以恢复原始的'report.git'存储库(就像例如alinrus wrote)。


假设你使用了默认配置和现代Git,这意味着“单独的遥控器”布局,你将在'remotes / origin'(或'remotes / report')命名空间中拥有'report.git'的远程跟踪分支。

要重新创建'report.git'存储库,您必须从远程跟踪分支推送(或获取)一次到普通分支,恢复普通的fetch refspec(至少对于分支:标记始终是镜像的)。因此,一次推送(或获取)的refspec类似于以下内容:refs/remotes/origin/*:refs/heads/*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'的oridinary(非裸和非镜像)克隆:

$ 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

现在我们恢复'report.git'的状态,它在'cloned / .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 HEADgit checkout。 git'你自己),但它是一个裸存储库,不是吗?

HTH(希望有帮助)。

相关问题