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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/895819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 06:29:13  来源:igfitidea点击:

What's the most straightforward way to clone an empty, *bare* git repository?

gitclonegit-bare

提问by Norman Ramsey

I've just finished cruising the Google search results that contain all the email rants about how stupid it is that git can't clone an empty repository. Some kind soul even submitted a patch. Until git is upgraded, what is the simplest, most straightforward method to clone an empty, baregit repository?

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

The ideal solution will support the -ooption to give the remote repo a name other than origin, and it will be implementable as a simple shell script, e.g., git-clone-empty-repo.

理想的解决方案将支持-o给远程回购以外的名称选项origin,这将是实现的一个简单的shell脚本,例如git-clone-empty-repo

(Why I want to do this: I've set up a bare, empty git repo on our NetApp filer where it will be backed up, but I want to work with a clone on my local hard drive and push and pull back and forth. Other people I work with will be doing the same. I create new git repos a lot and my inability to clone an empty repo makes me crazy.)

(为什么我要这样做:我已经在我们的 NetApp 文件管理器上设置了一个空的 git repo . 和我一起工作的其他人也会这样做。我经常创建新的 git 存储库,但我无法克隆空存储库让我发疯。)



EDIT: VonC's thread suggests that

编辑:VonC 的帖子表明

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

is equivalent to cloning the remote repo when the repo is empty. This is not quite what I want because I always use the -ooption with git clone; I name the remote repo according to what machine it is on or some other memorable criterion. (I have too many repos to keep them straight if they're all called origin.)

相当于当repo为空时克隆远程repo。这不是我想要的,因为我总是使用-ogit clone的选项;我根据远程仓库所在的机器或其他一些令人难忘的标准命名远程仓库。(如果他们都被称为 ,我有太多的回购来保持他们的直线origin。)



EDIT: The following answer will be marked accepted :-)

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

To clone an empty, bare repo at path,

要在path克隆一个空的裸仓库,

  1. Keep at ~/git/onefilea non-bare git repo containing one innocuous file such as .gitignore. (Alternatively, create such a repo dynamically.)
  2. (cd ~/git/onefile; git pushpathmaster)
  3. git clone -onamepath
  1. 保存在~/git/onefile包含一个无害文件的非裸 git 存储库中,例如 .gitignore 文件.gitignore。(或者,动态创建这样的存储库。)
  2. (cd ~/git/onefile; git push小路master)
  3. git clone -o名称路径

In other words, don't attempt to clone the empty repo, but rather after creating it, push to it a simple repo containing one innocuous file. Then it is no longer empty and can be cloned.

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

If someone does not beat me to it, I will post a shell script.

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

采纳答案by VonC

May be just have a git repo with the minimum number of file in it:

可能只有一个 git repo,其中包含最少数量的文件:

one: .gitignorewith obvious ignore patterns in it.

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

And then clone that "almost empty" repository.
Note that such an "almost empty" repository ("almost" because it still has a minimal working directory alongside the .git directory) can then by 'git clone --bare' (as illustrated here), making it a true bare repo (but not an "empty" one).
This is that bare repo you can then:

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

  • clone everywhere you want.
  • or (more importantly) push to (since it is a bare repo)
  • 克隆你想要的任何地方。
  • 或者(更重要的是)push to(因为它是一个裸仓库)


You have in this threada good summary of the "other way around" (which I keep here for reference).

您在此线程中对“其他方式”进行了很好的总结(我保留在这里以供参考)。

$ 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.

对于一个新项目(还没有代码),我想
在远程公共服务器上创建一个空的裸仓库(没有工作副本)作为起点
,在本地克隆它,然后逐渐在本地创建内容并将其
推送到远程,公共服务器。

To which Junio C Hamano responded:

对此 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.

您为发布准备了一个空的裸存储库,这非常好。

下一步是在别处准备内容。那将是您的私人工作场所,即您通常会在其中工作的地方)。
您从您的私人工作场所推送到该发布存储库。
你的工作地点是最初提交的地方,因为你是启动项目的人。

请注意,私人工作场所不必是空工作场所的克隆。那实际上是倒退。你的工作从你的私人工作场所开始到出版场所。

如果您愿意,您甚至可以克隆您的私有存储库以发布一个,以明确谁是主存储库,谁是副本,但由于您已经拥有用于发布的裸存储库,因此只需将其推入即可。