如何将 git 子模块文件夹 `.git` 发送到父存储库 `.git/modules/` 文件夹?

How to send a git submodule folder `.git` to the parent repository `.git/modules/` folder?

当您在本地创建 git 个子模块时,如本脚本所示:

# Create the directory structure
mkdir main_repo
mkdir main_repo/unrelated_repo
mkdir main_repo/unrelated_repo/main_repo_submodule

cd main_repo

# Setup the unrelated_repo
cd unrelated_repo
printf "# UnRelated Repository\n\n" > README.md
git init
git add -f README.md
git commit -m "Added the unrelated repository first commit."
git remote add origin https://github.com/user/unrelated_repo

# Setup the main_repo_submodule
cd main_repo_submodule
printf "# Main Repository Submodule\n\nThis is a submodule from the \`main_repo\`, and not from the \`unrelated_repo\`\n" > README.md
git init
git add -f README.md
git commit -m "Added the main repository submodule first commit."
git remote add origin https://github.com/user/main_repo_submodule

# Setup the main_repo
cd ../..
printf "# Main Repo\n\nThis is the main repository which contains submodules\n" > README.md
printf "\nThis is a main_repo_file on the unrelated repository\n\n" > unrelated_repo/main_repo_file.txt
printf "\n*\n**\n\n" > unrelated_repo/.gitignore
git init
git add -f README.md unrelated_repo/main_repo_file.txt unrelated_repo/.gitignore
git submodule add -f -- https://github.com/user/main_repo_submodule "unrelated_repo/main_repo_submodule"

git commit -m "Added the first main repository first commit."
git remote add origin https://github.com/user/main_repo

子模块 .git 文件夹将位于以下文件夹中:

  1. main_repo/unrelated_repo/main_repo_submodule/.git/

但是,如果我将此子模块推送到远程服务器,请执行以下操作:

  1. git clone --recursive

子模块 git 文件夹 main_repo/unrelated_repo/main_repo_submodule/.git/ 将位于:

  1. main_repo/.git/modules/main_repo_submodule/

在它的位置上将有一个指向父存储库中 git 文件夹的同链接。那么如何将一个子模块放在这个父文件夹 .git/modules/ 文件夹中,而不是递归地克隆它?

这是必要的,因为我想将我的子模块文件夹保留在父模块文件夹中,保持工作区干净,而不必在每次添加新子模块时递归地重新克隆所有内容。

更新

我试过:

# Move the submodule to the parent repository
mkdir -p .git/modules
# shopt -s dotglob
mv unrelated_repo/main_repo_submodule/.git/ .git/modules
printf "gitdir: ../../.git/modules/main_repo_submodule\n" > unrelated_repo/main_repo_submodule/.git

但是git说找不到子模块。

之后

mv unrelated_repo/main_repo_submodule/.git/ .git/modules

你还需要

mv .git/modules/.git .git/modules/main_repo_submodule