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

问题描述 投票:0回答:1

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

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

这个存储库需要拆分。文件夹 dbscripts 需要拆分到它们自己的存储库中,我知道这可以通过

git filter-branch --subdirectory-filter ...

轻松完成

我还需要根据当前结构(包括历史记录)创建一个新的主存储库,但排除任何已拆分到其自己的存储库的内容要排除的特定文件列表中的任何文件:

 file1
file3
even_more_stuff
。有没有办法使用
git filter-branch
过滤掉特定名称的文件?

最终的主存储库应该是:

src/
other_stuff/
file2

其中一个问题是我不应该对原始存储库进行任何更改,因此我不能只删除

file
file3
等...然后将其余部分复制到新存储库。

git git-filter-branch
1个回答
15
投票

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

的方法
  1. 安装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
更好。

© www.soinside.com 2019 - 2024. All rights reserved.