如何获取Git仓库中control子文件夹的提交历史并迁移至新远程仓库
迁移单个Git文件夹并保留提交历史的操作方案
推荐方法:使用git filter-repo(替代过时的git filter-branch)
- 先克隆原仓库到临时目录(已有本地仓库可跳过)
git clone /ship/tools tools-temp cd tools-temp - 过滤仓库,仅保留
control文件夹的提交历史,并将其设为仓库根目录git filter-repo --path control --path-rename control/: - 添加新的远程仓库地址
git remote add new-origin <你的新远程仓库路径/URL> - 推送过滤后的代码到新仓库
git push -u new-origin main # 分支名根据实际情况替换,比如master
备选方法:使用git subtree split(适合需保留原仓库关联的场景)
- 进入原
tools仓库目录cd /path/to/existing/tools - 将
control文件夹拆分为独立分支git subtree split --prefix=control --branch control-only - 创建新的本地仓库并拉取拆分后的分支
mkdir new-control-repo cd new-control-repo git init git pull /path/to/existing/tools control-only - 关联新远程仓库并推送
git remote add origin <你的新远程仓库路径/URL> git push -u origin main
注意事项
- 操作前一定要备份原仓库,防止数据丢失
- 若原仓库有多个分支,需对每个分支重复上述过滤/拆分操作
git filter-repo部分Git版本未默认集成,可通过pip安装:pip install git-filter-repo
内容的提问来源于stack exchange,提问作者user15150033
相关产品推荐
相关产品推荐

