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

时间:2017-06-20 18:07:17

标签: git recursion git-submodules git-clone

当您在本地创建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/
  2. 但是,如果我将此子模块推送到远程服务器,请执行:

    1. git clone --recursive
    2. 子模块git文件夹main_repo/unrelated_repo/main_repo_submodule/.git/将位于:

      1. main_repo/.git/modules/main_repo_submodule/
      2. 它的位置将是父存储库上其git文件夹的synlink。那么如何在这个父文件夹.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说找不到子模块。

1 个答案:

答案 0 :(得分:1)

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

你还需要

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