把某个git子目录迁移到新repo的根目录
对项目重构时有这样一个需求,1)要把代码库某个目录下的所有代码作为一个新代码库的根目录,2)并且之前所有的代码提交记录要一并迁移到这个新的 git repo。
当你尝试用 git filter-branch --subdirectory-filter YOUR_SUB_DIR -- --all
来解决问题时,会看到一个警告推荐我们使用 git filter-repo。它是一个用于重写 git history 的多功能小工具,用法参考filter-repo 使用手册。我们的需求在这里只是它的一个小 case。
下面直接列出操作步骤:
1)安装 git-filter-repo
brew install git-filter-repo
2)Clone 原来的 Repo
mkdir codebase
cd codebase
git clone YOUR_GIT_REPO_URL/myProject
cd myProject
3) 拉取所有信息到本地
git fetch --all
git pull --all
4)执行 filter-repo 命令,让某个子目录成为新 repo 的根目录。
git filter-repo --subdirectory-filter The_SubDir_in_myProject
5) 在 github/gitlab 创建一个新 repo,把这个 repo 设为这个子目录的 remote 目标
git remote add origin YOUR_NEW_REPO_GIT_URL
6) 把 master 的 history push 到新 repo
git branch -M master
git push -uf origin master
7)把所有 branchs/tags 都 push 上去
git push --all origin
Reference:
[1]: https://github.com/newren/git-filter-repo/