git - 如何从现有目录结构生成 git superproject

标签 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 存储库(例如 50-100 个),它们可以位于树内的任何位置,并且它们可以来自不同的服务器,具有不同的配置。

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

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

所以基本上我只是希望 projects 目录成为一个 super 项目,并保持其他所有内容完好无损,因为它们已经为我设置好了。

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

最佳答案

我需要相同的答案,所以我为 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 superproject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20665274/

相关文章:

.net - NuGet 和 Git 子模块

git - repo 克隆时清空 Git 子模块文件夹

git - Git使用另一个 merge 策略选项重新 merge 文件?

git - 如何提升/ff分支到HEAD

git - 如何避免在 git 中进入分离的头部状态?

git - AWS Elastic Beanstalk + Git 子模块

git - 如何在 git 中的特定哈希(提交)处 checkout 代码

自定义 SSH 端口上的 Gitlab

git - 从 .gitmodules 恢复 git 子模块

git - 添加包含另一个子模块的 git 子模块?