将Git子模块转换为子树 [英] Convert Git submodule to subtree

查看:526
本文介绍了将Git子模块转换为子树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I convert a git submodule (with a folder in the local file system as a remote) to a git subtree, preferably preserving the commit history of the submodule?

推荐答案

以下bash脚本基于Alexander Mikhailian的帖子( http: //mikhailian.mova.org/node/233 )。我稍微修改它以调用子树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

The following bash script is based on Alexander Mikhailian's post (http://mikhailian.mova.org/node/233). I modified it slightly to call subtree add instead of read-tree. It will fetch the list of submodules from the .gitmodule and extract the module's prefix, name and url. It then removes each submodule and adds them back as a subtree at the same location. It also adds the remote of each submodule as a remote so you can update the subtree by providing its name instead of its url later on (i.e. git subtree pull -P Foo Foo master --squash instead of git subtree pull -P Foo https://example.com/foo.git master --squash)

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

You may remove the --squash argument if you want to import the full history of the subtree to your repository. With --squash, will only import the head of the subtree into your repository. This should probably be what most people want.

有关更多信息,您可能需要阅读atlassian的这篇文章: http://blogs.atlassian.com/2013/05/alternatives-to-git-submodule-git- subtree /

For more information, you may want to read this post by 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

这篇关于将Git子模块转换为子树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆