没有遥控器的嵌套 git 存储库(又名没有遥控器的 git 子模块)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6100966/
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 10:58:43  来源:igfitidea点击:

Nested git repositories without remotes (a.k.a. git submodule without remotes)

gitgit-submodules

提问by David Alan Hjelle

I have a project of which I am interested in breaking out portions as open-source. I've set up a "main" git repository (say, in a directory "main") and sub-repositories in "main/one", "main/two", "main/three". I thought that by going into "main" and doing

我有一个项目,我有兴趣将其中的部分分解为开源。我已经在“main/one”、“main/two”、“main/three”中设置了一个“main”git存储库(比如在目录“main”中)和子存储库。我认为通过进入“主要”并做

git add one
git add two
git add three

(note the lack of trailing slashes), I'd set up submodules with the sub-repositories and be good to go.

(注意缺少尾部斜杠),我会用子存储库设置子模块,然后就可以开始了。

However, as noted in Git - how to track untracked content?, this only creates gitlinks and not real submodules.

但是,正如Git 中所述 - 如何跟踪未跟踪的内容?,这只会创建 gitlinks 而不是真正的子模块。

Unfortunately, that answer doesn't help, as it assumes that there is a "master" repository somewhere elsefor "main/one", "main/two", and "main/three". I'd like these sub-repo's to bethe master repositories. I'm considering fake submodules (as per Git fake submodules), but that's not a particularly ideal situation for cloning.

不幸的是,这个答案没有帮助,因为它假设在其他地方有一个“主”存储库,用于“主/一”、“主/二”和“主/三”。我希望这些子存储库成为主存储库。我正在考虑使用假子模块(根据Git fake submodules),但这并不是克隆的特别理想情况。

Any other suggestions out there?

还有其他建议吗?

采纳答案by Chris Johnsen

You can do what you want, but your one, two, and threewould need to be accessible to whoever will need to clone them—this is not usually the case for “random”?development repositories.

你可以做你想做的事,但是你的one, two, 和three需要被需要克隆它们的人访问 - 这通常不是“随机”开发存储库的情况。

If you do set this up, you will need to be very careful not to delete “your” repository (or make it otherwise inaccessible) since it is not just “yours”: it will be originin your collaborator's clones and it will serve as the “central”/“upstream” repository (as specified in .gitmodules).

如果您进行了设置,则需要非常小心,不要删除“您的”存储库(或使其无法访问),因为它不仅仅是“您的”存储库:它将是您合作者克隆的起源,并将用作“中央”/“上游”存储库(在 中指定.gitmodules)。



If all your collaborators are local (and can read from the repositories), you can add your existing sub-repositories as submodules by giving their local paths as URLs:

如果您的所有协作者都是本地的(并且可以从存储库中读取),您可以通过将其本地路径作为 URL 来添加现有的子存储库作为子模块:

git rm --cached one two three
git submodule add `pwd`/one
git submodule add `pwd`/two
git submodule add `pwd`/three

If not all your collaborators will be working on the same machine, then that will probably not work correctly (since it will store a local path in .gitmodules; non-local collaborators would have to adjust the URLs after git submodule init).

如果不是所有的协作者都在同一台机器上工作,那么这可能无法正常工作(因为它将在 中存储本地路径.gitmodules;非本地协作者必须在 之后调整 URL git submodule init)。

If your one, two, and threeare remotely Git-accessible, then you can specify their effecive URLs instead:

如果您的one, two, 和three是远程 Git 可访问的,那么您可以指定它们的有效 URL:

git rm --cached one two three
git submodule add server:/path/to/your/main/one
git submodule add server:/path/to/your/main/two
git submodule add server:/path/to/your/main/three

In both cases, since you already have a sub-repository in place, git submodule addwill use that instead of trying to clone from the specified path/URL.

在这两种情况下,由于您已经有一个子存储库,git submodule add将使用它而不是尝试从指定的路径/URL 进行克隆。

回答by VonC

Chris's answerdon't assume for a "master" repo (masterbeing the default name of the main branch in Git, not a repo).

Chris回答不假定为 " master" 存储库(master是 Git 中主分支的默认名称,而不是存储库)。

It declares submodules in a "parent" repo, which in your case would be "main".

它在“ parent” repo 中声明子模块,在您的情况下是“ main”。

So you need three independent repo "one" "two" and "three" setup elsewhere, and then clone and add them to your "main" repo as submodules:

所以你需要三个独立的 repo " one" " two" 和 " three"在别处设置,然后将它们克隆并main作为子模块添加到你的 " " 仓库中:

# first remove the one directory previously added
git rm --cached one
# then declare the external one repo as a submodule in "main" repo)
git submodule add /path/to/one.git one

回答by d-nnis

I just came up with the idea to place the repository of the submodules exactly as a subdirectory of the superproject. That way, the project as a whole is only dependent on the one repository/ location that you set up (based on the answer from Chris).

我刚刚想出了将子模块的存储库完全作为超级项目的子目录放置的想法。这样,整个项目仅依赖于您设置的一个存储库/位置(基于 Chris 的回答)。

E.g. Submodule onewould have as its main repository the location server:/path/to/your/main/one(via git remote -vin that dir).

例如,子模块one会将位置server:/path/to/your/main/one(通过git remote -v该目录)作为其主要存储库。

That way the submodulefunctionality (e.g. git submodule update --recursive) is pointed to the right location as well since the URL of that module/repository points to ./one(.gitmodules).

这样,submodule功能(例如git submodule update --recursive)也指向正确的位置,因为该模块/存储库的 URL 指向./one(.gitmodules)。