有选择地将文件和目录移动到新的仓库(保留历史记录)

Selectively move files & directories to new repo (preserving history)

我有一个结构如下的项目:

src/
scripts/
db/
other_stuff/
even_more_stuff/
file1
file2
file3

此存储库需要拆分。数据库和脚本需要拆分到它们自己的存储库中,我知道这可以通过 git filter-branch --subdirectory-filter ...

轻松完成

我还需要根据当前结构(包括历史)创建一个新的主存储库,但是排除任何已经拆分到自己的存储库的东西 OR 要排除的特定文件列表中的任何文件:file1file3even_more_stuff。有没有办法使用 git filter-branch 按特定名称过滤掉文件?

生成的主回购应该只是:

src/
other_stuff/
file2

一个问题是我不应该对原始存储库进行任何更改,所以我不能只删除 filefile3 等...然后复制剩余部分到新存储库。

好的,这是我使用 git-filter-repo 的方法:

  1. 安装 git-filter-repo ( https://github.com/newren/git-filter-repo )
  2. 新鲜克隆源代码库
  3. cd my-source-repo
  4. git filter-repo --path src --path other-stuff --path file2
  5. 检查结果,剩余文件 git log 看起来没问题。
  6. 克隆目标存储库(我的是空的)
  7. cd my-target-repo
  8. 将源添加为目标的远程:git remote add src-repo path/to/my-source-repo
  9. 从源存储库中提取:git pull src-repo main --allow-unrelated-histories
  10. 清理远程:git remote rm src-repo
  11. 推到原点:git push --set-upstream origin main

(适当更改分支名称)

我只做过一次,所以我不知道是否有更短、更简单的方法,但这仍然比使用 git filter-branch 更好。