如何在将两个Rust应用移入rust workspace时保留Git提交历史?
迁移两个Git仓库到Rust工作区并保留提交历史
可以通过Git的filter-repo工具重写原有仓库的提交历史,将文件迁移到工作区的子目录下,完整保留提交记录。具体步骤如下:
1. 创建并初始化工作区仓库
mkdir w && cd w git init
2. 迁移a1仓库的历史到w/a1目录
# 添加a1仓库作为远程源 git remote add a1 ../a1.git # 替换为你的a1.git实际路径或远程URL # 拉取a1的所有提交历史 git fetch a1 # 重写a1的所有提交,将所有文件迁移到a1子目录 git filter-repo --to-subdirectory-filter a1 --refs=refs/remotes/a1/*
3. 迁移a2仓库的历史到w/a2目录
git remote add a2 ../a2.git # 替换为你的a2.git实际路径或远程URL git fetch a2 git filter-repo --to-subdirectory-filter a2 --refs=refs/remotes/a2/*
4. 配置Rust工作区
创建工作区根目录的Cargo.toml:
[workspace] members = [ "a1", "a2" # 后续添加d1、dn等新crate时,在这里补充路径 ]
提交工作区配置:
git add Cargo.toml git commit -m "Initialize Rust workspace configuration"
5. 推送到新的远程仓库apps.git
git remote add origin /path/to/apps.git # 替换为你的apps.git实际路径或远程URL git push -u origin main # 替换为你的主分支名称,如master
注意事项
- 确保本地安装了
git filter-repo(Git 2.22及以上版本默认包含,低版本需单独安装) - 如果原仓库存在多个分支,需调整
--refs参数指定要迁移的分支,或批量处理所有分支 - 迁移完成后,原仓库的所有提交历史会完整保留在新仓库的对应子目录下,可通过
git log -- a1查看a1的历史提交 - 后续新增d1、dn等crate时,直接在w目录下创建,更新
Cargo.toml的members列表即可
内容的提问来源于stack exchange,提问作者BitTickler
相关产品推荐
相关产品推荐

