git - 克隆空的、*裸* git 存储库的最直接方法是什么?

标签 git clone git-bare

我刚刚浏览完 Google 搜索结果,其中包含所有关于 git 无法克隆空存储库是多么愚蠢的电子邮件咆哮。一些善良的灵魂甚至提交了一个补丁。在升级 git 之前,克隆空的、 git 存储库的最简单、最直接的方法是什么?

理想的解决方案将支持 -o 选项来为远程仓库指定一个不同于 origin 的名称,并且它可以作为一个简单的 shell 脚本来实现,例如, git-clone-empty-repo.

(我为什么要这样做:我已经在我们的 NetApp 文件管理器上设置了一个空的 git 存储库,它将被备份,但我想在我的本地硬盘驱动器上使用一个克隆并推 pull 来回。和我一起工作的其他人也会做同样的事情。我经常创建新的 git 仓库,但我无法克隆一个空的仓库让我抓狂。)


编辑:VonC 的帖子表明

$ git-init
$ git-remote add origin server:/pub/git/test.git

相当于在repo为空的情况下克隆远程repo。这不是我想要的,因为我总是在 git clone 中使用 -o 选项;我根据远程仓库所在的机器或其他一些令人难忘的标准来命名远程仓库。 (如果它们都被称为 origin,我有太多的 repos 无法让它们保持直截了当。)


编辑:以下答案将被标记为已接受:-)

要在路径 克隆一个空的、裸露的仓库,

  1. ~/git/onefile 中保留一个非裸 git 存储库,其中包含一个无害的文件,例如 .gitignore。 (或者,动态创建这样的存储库。)
  2. (cd ~/git/onefile; git push path master)
  3. git clone -o 名称 路径

换句话说,不要试图克隆空的 repo,而是在创建它之后,将一个包含一个无害文件的简单 repo 推送给它。然后它不再是空的,可以被克隆。

如果有人不反对我,我会发布一个 shell 脚本。

最佳答案

可能只有一个包含最少文件数的 git 仓库:

一个:.gitignore,其中有明显的忽略模式。

然后克隆那个“几乎是空的”存储库。
请注意,这样一个“几乎是空的”存储库(“几乎”是因为它在 .git 目录旁边仍然有一个最小的工作目录)然后可以通过 'git clone --bare'(如 illustrated here ) ,使其成为真正的裸仓库(但不是“空”仓库)。
这是你可以然后的裸 repo :

  • 克隆任何你想要的地方。
  • 或者(更重要的是)推送到(因为它是一个裸仓库)

你有 this thread “另一种方式”的一个很好的总结(我保留在这里以供引用)。

$ git-init
$ git-remote add origin server:/pub/git/test.git

For a new project (no code yet) I wanted to make an empty, bare
repository (no working copy) on a remote public server as a starting
point, clone it locally, and gradually create content locally and
push it out to the remote, public server.

Junio C Hamano 回应:

You prepared an empty bare repository for publishing, and that is very good.

The next step is that you prepare your contents elsewhere. That would be your private working place, i.e. the place you would normally work in).
You push from your private working place into that publishing repository.
Your working place is where the very initial commit should come from, since you are the one who is starting the project.

Note that the private working place does not have to be a clone of the empty one. That actually is backwards. Your work started from your private working place to the publishing one.

You could even clone your private repository to publishing one to make it clear who is the master and who is the copy if you wanted to, but because you already have the bare repository for publishing, just pushing into it is all that is needed.

关于git - 克隆空的、*裸* git 存储库的最直接方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/895819/

相关文章:

git - 将 git .gitignore 规则应用于现有存储库

git - 从 git repo 克隆并提交到新的存储库

Git 分支工作流程困境

jquery - jquery clone() 之后 addthis-button 丢失 click-eventhandler

c++ - 复制构造函数被隐式删除,因为默认定义的格式不正确

git - 获取裸存储库的工作副本

git - git中 'tracking'的概念有不同的含义吗?

java - 尝试克隆数组时出现 ArrayIndexOutOfBoundsExcetion

git - 使用裸存储库 Hook 将服务器恢复到过去的提交

Git 裸仓库有 master 分支 checkout 吗?