将Git子模块转换为子树

时间:2015-01-29 12:52:05

标签: git git-submodules git-subtree

如何将git子模块(本地文件系统中的文件夹作为远程)转换为git子树,最好保留子模块的提交历史记录?

2 个答案:

答案 0 :(得分:11)

以下bash脚本基于Alexander Mikhailian的帖子(http://mikhailian.mova.org/node/233)。我稍微对其进行了修改,以便调用subtree add而不是read-tree。它将从.gitmodule获取子模块列表,并提取模块的前缀,名称和URL。然后删除每个子模块并将它们作为子树添加回同一位置。它还将每个子模块的远程添加为远程,因此您可以通过稍后提供其名称而不是其URL来更新子树(即git subtree pull -P Foo Foo master --squash而不是git subtree pull -P Foo https://example.com/foo.git master --squash

如果要将子树的完整历史记录导入存储库,则可以删除--squash参数。使用--squash,只会将子树的头部导入到您的存储库中。这应该是大多数人想要的。

有关详情,请访问atlassian:http://blogs.atlassian.com/2013/05/alternatives-to-git-submodule-git-subtree/

#!/bin/bash -x
# extract the list of submodules from .gitmodule
cat .gitmodules |while read i
do
if [[ $i == \[submodule* ]]; then
    echo converting $i

    # extract the module's prefix
    mpath=$(echo $i | cut -d\" -f2)

    # skip two lines
    read i; read i;

    # extract the url of the submodule
    murl=$(echo $i|cut -d\= -f2|xargs)

    # extract the module name
    mname=$(basename $mpath)

    # deinit the module
    git submodule deinit $mpath

    # remove the module from git
    git rm -r --cached $mpath

    # remove the module from the filesystem
    rm -rf $mpath

    # commit the change
    git commit -m "Removed $mpath submodule"

    # add the remote
    git remote add -f $mname $murl

    # add the subtree
    git subtree add --prefix $mpath $mname master --squash

    # fetch the files
    git fetch $murl master
fi
done
git rm .gitmodules

答案 1 :(得分:6)

由@GaspardP修改的Alexander Mikhailian的剧本对我不起作用。

我修改了它,并做了改进。现在,新的子树将指向与旧子模块相同的提交。以前,该脚本只会从目标存储库下载最新的提交,从而可能导致兼容性问题。

https://gist.github.com/Nikita240/0c98cea8f53a15e69699cd8bc40657c4

#!/bin/bash -x
# This script will convert all your git submodules into git subtrees.
# This script ensures that your new subtrees point to the same commits as the
# old submodules did, unlike most other scripts that do this.
# THIS SCRIPT MUST BE PLACED OUTSIDE OF YOUR REPOSITORY!!!!!!!!!!
# Otherwise, the script will interfere with the git commits.
# Save the script in your home directory as `~/subtrees.sh`
# `cd` into your repository
# Run `~/subtrees.sh`
# Enjoy!

# extract the list of submodules from .gitmodule
cat .gitmodules |while read i
do
if [[ $i == \[submodule* ]]; then
    echo converting $i

    read i

    # extract the module's prefix
    mpath=$(echo $i | grep -E "(\S+)$" -o)

    echo path: $mpath

    read i

    # extract the url of the submodule
    murl=$(echo $i|cut -d\= -f2|xargs)

    echo url: $murl

    # extract the module name
    mname=$(basename $mpath)

    echo name: $mname

    # extract the referenced commit
    mcommit=$(git submodule status $mpath | grep -E "\S+" -o | head -1)

    echo commit: $mcommit

    # deinit the module
    git submodule deinit $mpath

    # remove the module from git
    git rm -r --cached $mpath

    # remove the module from the filesystem
    rm -rf $mpath

    # commit the change
    git commit -m "Removed $mpath submodule at commit $mcommit"

    # add the remote
    git remote add -f $mname $murl

    # add the subtree
    git subtree add --prefix $mpath $mcommit --squash

    # commit any left over uncommited changes
    git commit -a -m "$mname cleaned up"

    # fetch the files
    git fetch $murl master

    echo
fi
done
git rm .gitmodules
git commit -a -m "Removed .gitmodules"