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

问题描述 投票:0回答:4

我有一个项目,我有兴趣将其部分内容开源。我已经设置了嵌套 git 存储库主要、一、二和三:

main/
├── one
├── three
└── two

我认为通过进入“主要”并执行

git add one
git add two
git add three

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

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

不幸的是,这个答案没有帮助,因为它假设某个地方有一个“主”存储库else用于“main/one”,“main/two”和“main/two”。我希望这些子存储库能够成为主存储库。我正在考虑假子模块(按照Git假子模块),但这对于克隆来说并不是特别理想的情况。 还有其他建议吗?

git git-submodules
4个回答
35
投票
one

two
three
需要可供任何需要克隆它们的人访问 - 对于“随机”开发存储库来说通常情况并非如此。
如果您确实进行了此设置,则需要非常小心,不要删除“您的”存储库(或使其无法访问),因为它不仅仅是“您的”:它将是您协作者克隆中的

origin

,并且它将充当“中央”/“上游”存储库(如.gitmodules中指定)。


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

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

如果并非所有协作者都在同一台计算机上工作,那么这可能无法正常工作(因为它将在 
.gitmodules

中存储本地路径;非本地协作者必须在

git submodule init
之后调整 URL)。
如果您的 

one

two
three
可以远程 Git 访问,那么您可以指定它们的有效 URL:
git rm --cached one two three
git -c protocol.file.allow=always submodule add server:/path/to/your/main/one
git -c protocol.file.allow=always submodule add server:/path/to/your/main/two
git -c protocol.file.allow=always submodule add server:/path/to/your/main/three

在这两种情况下,由于您已经有了一个子存储库,因此 
git submodule add

将使用它,而不是尝试从指定的路径/URL 克隆。

    


6
投票
Chris

answer不要假设“master”存储库(

master
是Git中主分支的默认名称,而不是存储库)。

它在“

parent

”存储库中声明子模块,在您的情况下将是“

main
”。

因此,您需要三个独立的存储库“

one

”“

two
”和“
three
”设置
elsewhere
,然后克隆并将它们作为子模块添加到您的“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



1
投票

例如子模块

one

将把位置

server:/path/to/your/main/one
作为其主存储库(通过该目录中的
git remote -v
)。

这样,

submodule

功能(例如

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


0
投票

$ git submodule add ./path/to/submodule path/to/submodule Adding existing repo at 'path/to/submodule' to the index

有关我的用例的更多详细信息:
我想开始使用 git 跟踪现有文件夹,并且该文件夹已经包含存储库。我想将所有内容保留在本地:我没有在这些存储库上与任何人合作,并且这些文件夹已经以其他方式保存在云中。


© www.soinside.com 2019 - 2024. All rights reserved.