三个脚本

Alexander Mikhailian

cat .gitmodules |while read i
do
if [[ $i == \[submodule* ]]; then
mpath=$(echo $i | cut -d\" -f2)
read i; read i;
murl=$(echo $i|cut -d\ -f3)
mcommit=`eval "git submodule status ${mpath} |cut -d\ -f2"`
mname=$(basename $mpath)
echo -e "$name\t$mpath\t$murl\t$mcommit"
git submodule deinit $mpath
git rm -r --cached $mpath
rm -rf $mpath
git remote add $mname $murl
git fetch $mname
git branch _$mname $mcommit
git read-tree --prefix=$mpath/ -u _$mname
fi
done
git rm .gitmodules

Warning:

下文的两个脚本, 写死了 branch 是 master, 如果主分支不是 master, 需要做相应修改.

Nikita240 - Stack Overflow

Reference:

我对它进行了修改和改进。现在,新的subtree将指向与旧 submodule 相同的提交。以前,脚本只是从目标存储库下载最新的提交,这可能会导致兼容性问题。

#!/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"

GaspardP - Stack Overflow

Reference:

我稍微修改了一下,调用subtree add而不是read-tree。它将从.gitmodule中获取submodule的列表,并提取模块的前缀、名称和网址。然后它删除每个 submodule,并在同一位置添加它们作为subtree。它还将每个submodule的remote添加为remote,这样你就可以通过提供它的名字而不是它的网址来更新subtree了(即git subtree pull -P Foo Foo master --squash而不是git subtree pull -P Foo https://example.com/foo.git master --squash)。

如果你想把subtree的全部历史导入你的版本库,你可以去掉 --squash 参数。使用 --squash,将只导入subtree的 HEAD 到你的版本库。这可能是大多数人想要的。

#!/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

️参考文档

三人行, 必有我师; 知识共享, 天下为公. 本文由东风微鸣技术博客 EWhisper.cn 编写.

最新文章

  1. TortoiseGit 文件比对工具使用 Beyond Compare 和 DiffMerge
  2. 【2016.3.30项目技术记录】]VS2010自动生成MFC单文档框架程序的修改:去除属性框,在CViewTree类中添加鼠标单击响应
  3. Solr4:数据导入(dataimport)时,不符合Solr日期类型要求的字段的处理
  4. Linux下中文显示乱码问题
  5. CSS 清除浮动的四种方法
  6. 【转】Windows环境下Android NDK环境搭建
  7. HTML5下通过response header解决跨域AJAX cookie的问题
  8. 理解O/R Mapping
  9. iOS学习之页面之间传值的方式总结
  10. ZooKeeper搭建
  11. java实现——003二维数组中的查找
  12. 创建hbase-indexer出现 0 running
  13. win7系统IE浏览器主页被搜狗篡改问题的解决方法
  14. js中if语句的几种优化代码写法
  15. vim 私人快捷键备忘录
  16. Spring基础(3) : 静态工厂和实例工厂创建bean
  17. python进程join()函数理解
  18. 【ASP.NET】#001 获取服务器IP
  19. 那些代表性的HTTP状态码,你还只知道404吗?快来看看吧【转】
  20. android bitmap compress

热门文章

  1. Goland Socket 服务
  2. Invalid bound statement (not found)出现原因和解决方法
  3. 齐博x1钩子自动添加频道参数变量
  4. 微信支付v3接口的 官方 Java SDK
  5. pycharm系列---django
  6. 本地文件上传Gitee
  7. mysql删库报错
  8. laravel的_token传值 ; header中传_token
  9. 8、将两个字符串s1,s2进行比较,如果s1>s2,则输出一个正数。如果s1 = s2,输出零。如果s1 < s2, 输出一个负数,不用strcmp函数,输出的正数或者负数的绝对值应该是比较两字符串相应字符的ascii码的差值。
  10. ubuntu undefined reference to