在 git 远程存储库中添加子模块

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

在git中,我创建了一个像这样的顶级固件项目

mkdir firmware
cd firmware
git init --bare

cd ..
mkdir firmware_pci
cd firmware_pci
git init --bare

现在我想将firmware_pci作为子模块添加到固件中,所以我这样做了

cd ../firmware
git submodule add ../firmware_pci
fatal: /usr/libexec/git-core/git-submodule cannot be used without a working tree.

基本上,我想在firmware下创建多个firmware_xxx项目,所以当我递归克隆firmware时,它会将所有firmware_xxx项目克隆到一个名为firmware的目录中。我怎样才能实现这个目标?我可以在本地存储库上执行此操作,但在远程存储库上执行此操作时遇到困难。

根据 Julio PC 的反馈,我在远程系统中尝试了这个:

mkdir firmware
cd firmware
git init --bare

在我的本地系统上我这样做了:

git clone path/to/remote_repo
cd firmware
mkdir firmware_pci
cd firmware_pci
git init
touch delme.txt
git add .
git commit -m "first checkin"
cd ..
git submodule add ./firmware_pci
git add .
git commit -m "added submodule"
git push origin master

然后,在我的本地电脑上,我在不同的目录中执行了此操作

git clone --recursive path/to/remote_repo
Cloning into 'firmware'...
done.
Submodule 'firmware_pci' (/path/to/firmware/firmware_pci) registered for path 'firmware_pci'
fatal: repository '/path/to/firmware/firmware_pci' does not exist
fatal: clone of '/path/to/firmware/firmware_pci' into submodule path '/home/rash/git_temp2/firmware/firmware_pci' failed
Failed to clone 'firmware_pci'. Retry scheduled
fatal: repository '/path/to/firmware/firmware_pci' does not exist
fatal: clone of '/path/to/firmware/firmware_pci' into submodule path '/home/rash/git_temp2/firmware/firmware_pci' failed
Failed to clone 'firmware_pci' a second time, aborting

蒂亚

git git-submodules git-remote git-bare
1个回答
0
投票

根据您的示例:

mkdir firmware
cd firmware
git init --bare

git clone path/to/remote_repo
cd firmware
[...]
git submodule add ./firmware_pci

您似乎正在尝试将子模块直接添加到远程裸存储库,而不是在开发存储库上工作,跟踪您的远程,然后推送您的更改。

裸存储库不应该用来直接在它们上工作,而应该作为协作开发的权威基础。事实上,他们没有当前分支的概念,因为没有工作目录可以检查当前分支的副本并进行更改,如 @Julio P.C.评论里已经说过了

我假设您的工作(固件)已经包含在开发存储库中,您只需向其中添加一个或多个子模块并将这些子模块推送到其远程即可。在我的示例中,我重新创建了

firmware
firmware_pci
存储库作为开发存储库,只是为了演示,尽管在您的情况下它们已经存在。

#creating a mock firmware repo
mkdir firmware
cd firmware
git init
echo "some work" > firmware.txt
git add .
git commit -m "init commit"

#creating a mock firmware_pci repo
cd ..
mkdir firmware_pci
cd firmware_pci
git init
echo "some work" > firmware_pci.txt
git add .
git commit -m "init commit"

此时,我们可以为

firmware
firmware_pci
创建裸远程存储库,并将其更改推送给它们。随后,
firmware_pci
的远程将被添加为
firmware
的子模块,以便
firmware_pci
的两个版本(实际的repo和子模块)都可以与共同的权威基础(远程)跟踪和交换他们的工作.

#creating firmware's bare remote
cd ..
mkdir firmware_remote
cd firmware_remote
git init --bare

#pushing firmware changes to the remote
cd ../firmware
git remote add origin ../firmware_remote
git push -u origin master

#creating firmware_pci's bare remote
cd ..
mkdir firmware_pci_remote
cd firmware_pci_remote
git init --bare

#pushing firmware changes to the remote
cd ../firmware_pci
git remote add origin ../firmware_pci_remote
git push -u origin master

现在,我们可以将

firmware_pci
存储库添加为
firmware
的子模块,并将新子模块推送到
firmware
的远程。

#adding the new submodule
cd ../firmware
git submodule add ../firmware_pci_remote
git add .
git commit -m "add submodule"

#pushing firmware's changes (if there's any) along with the submodules' changes
git push --recurse-submodules=on-demand

最后,我们现在可以使用

firmware
选项克隆
--recurse-submodules
存储库。此版本的命令不仅克隆
firmware
存储库,而且还使用嵌套子模块初始化每个子模块并获取其数据。事实上,在能够获取子模块的内容之前,我们首先需要在存储库中初始化子模块。

cd ..
git clone firmware_remote --recurse-submodules firmware_clone
© www.soinside.com 2019 - 2024. All rights reserved.