如何从现有目录结构生成git超级项目

如何从现有目录结构生成git超级项目,git,git-submodules,Git,Git Submodules,我在一个巨大的目录树中设置了很多git项目。结构看起来像这样 projects projects/stuff -> this is a git repo projects/frontend/frontendone -> this is also a git repo projects/frontend/frontendtwo -> this is also a git repo projects/something -> this is a git repo ... 整

我在一个巨大的目录树中设置了很多git项目。结构看起来像这样

projects
projects/stuff -> this is a git repo
projects/frontend/frontendone -> this is also a git repo
projects/frontend/frontendtwo -> this is also a git repo
projects/something -> this is a git repo
...
整个树包含很多git repo(比如50-100),它们可以在树中的任何位置,也可以来自不同的服务器,配置不同

我想在
projects
目录中创建一个新的超级项目,其中包含作为子模块的所有存储库

我可以在git子模块上找到的大多数示例都是从没有git存储库开始的,然后使用
git submodule add
一个接一个地重新添加它们,但是我已经很好地设置了我的目录结构,一个接一个地重新做它们似乎太费劲了

基本上,我只希望
projects
目录成为一个超级项目,并将其他所有内容保持原样,因为它们已经很好地为我设置好了


创建超级项目最简单的方法是什么?

我需要同样的答案,所以我为Bash编写了一个脚本。如果你在不同的平台上,希望这能说明你需要做什么。干杯

#!/bin/bash
# add all the git folders below current folder as git submodules.
# NOTE: does not recursively nest submodules, but falls back to
# git submodule add's behavior of failing those.
# Workaround is to run this script in each affected directory
# from the bottom up.
# For example:
# a/.git
# b/.git
# b/b1/.git
# b/b2/.git
# c/.git
#
# run this script twice - first in b (adds b1 & b2 as submodules to b),
# then in root (adds a, b, c to root)
#

# if any options specified, treat as a test run and display only
if [ -z $1 ]; then
    GITSMADD="git submodule add -f"
    if [ ! -d ./.git ]; then
        git init
    fi
else
    GITSMADD="echo git submodule add -f"
    echo running in DISPLAY mode
fi

find . -name '.git' -type d -exec dirname {} \; | sort | while read LINE
do
    if [ "$LINE" != "." ]; then
        pushd $LINE > /dev/null
        ORIGIN=$(git remote -v | grep fetch | head -1 | awk '{print $1}')
        URL=$(git remote -v | grep fetch  | head -1 | awk '{print $2}')
        popd > /dev/null
        if [ -z $ORIGIN ]; then
            echo "Adding local folder $LINE as submodule."
            $GITSMADD "$LINE"
        else
            echo "Adding remote $URL as submodule in folder $LINE"
            $GITSMADD "$URL" "$LINE"
        fi

    fi
done

我认为如果没有添加
git子模块
,您将无法相处。尽管如此,您仍然可以使用
git子模块foreach
。然而,我发现问题中缺少的是,您是想将子模块保留在它们当前所在的位置,还是想将它们移到一个新的位置。@raina77ow我想将它们保留在那里(如果可能的话),因为这似乎是最简单的(对我来说)。虽然如果这不可能,从外部重新创建整个结构,然后将其移回此处也是一种选择,但是我希望能够不用太多的努力重新创建结构。