有没有办法重建子模块的.git目录?

时间:2017-01-21 06:18:57

标签: git git-submodules

我有一个带子模块的项目,我希望能够重建子模块的.git目录,就好像已经克隆了子模块一样。

更详细地说,我在git项目subproject中有一个子模块superproject,我已经完成了:

git clone git://url.of.superproject
cd superproject
git submodule update --init

subproject没有自己的.git目录,而是.git文件,指向目录../.git/modules/subproject。我真的需要.git的真实subproject目录,因为我使用的工具(Python的pip)坚持将subproject目录复制到任意位置以便对其进行处理,并且它将要做的工作包括运行git命令。

有没有办法重建subproject/.git目录,就好像它已被克隆一样?我不能只复制.git/modules/subproject目录,因为它包含指向superproject/.git目录其他部分的相对链接。

1 个答案:

答案 0 :(得分:1)

要回答我自己的问题 - git clone subproject将构建.git目录:

git clone --recursive subproject subproject-copy

现在subproject-copy/.git是一个完整的git目录。

这是一个bash函数,以更一般的方式执行此操作(正确重建origin遥控器等):

function fill_submodule {
    # Restores .git directory to submodule, if necessary
    local repo_dir="$1"
    [ -z "$repo_dir" ] && echo "repo_dir not defined" && exit 1
    local git_loc="$repo_dir/.git"
    # For ordinary submodule, .git is a file.
    [ -d "$git_loc" ] && return
    # Need to recreate .git directory for submodule
    local origin_url=$(cd "$repo_dir" && git config --get remote.origin.url)
    local repo_copy="$repo_dir-$RANDOM"
    git clone --recursive "$repo_dir" "$repo_copy"
    rm -rf "$repo_dir"
    mv "$repo_copy" "$repo_dir"
    (cd "$repo_dir" && git remote set-url origin $origin_url)
}

使用:

fill_submodule subproject