如何在维护与这些文件关联的提交的同时将文件从 RepoA 导入到 RepoB?

How to import files from RepoA to RepoB whilst maintaining the commits associated to these files?

RepoA 中,有一个目录 (directoryA),其中包含一些文件(file1file2file3),我想移动这些文件从 RepoARepoB.

的文件

这是RepoA:

RepoA
    directoryA
        file1
        file2
        file3
    directoryB
        file1
        file2
        file3

在我想做的事情之后,RepoB 看起来像:

RepoB
    directoryA
        file1
        file2
        file3

我可以从 RepoA 下载 directoryA 并将其上传到 RepoB,但我会丢失与 file1file2file3.

我也无法将整个 RepoA 转移到 RepoB(如在合并中),因为来自 directoryB 的提交与 RepoA 无关。

编辑:我尝试使用 git filter-branch --subdirectory-filter directoryName -- --all

它对我不起作用,每次我在执行该命令后尝试拉动时都会收到错误消息:

fatal: refusing to merge unrelated histories

首先,请新建一个分支并查看此过程,因为以下操作会修改本地RepoA的历史记录并删除一些文件:

在本地 RepoA,

(1) git filter-branch --subdirectory-filter directoryA <branch>
(2) mkdir directoryA
(3) mv * directoryA
(4) git add .
(5) git commit -am 'message'

在本地 RepoB,

(6) git remote add repoA <local RepoA’s path>
(7) git pull RepoA <branch>

如果在步骤 (7) 中出现错误:fatal: refusing to merge unrelated histories,您可以通过执行 git pull RepoA <branch> --allow-unrelated-histories

来忽略不相关的历史记录

然后 file1file2file3 的提交历史将显示在 RepoB 中。可以用gitk --all查看更清楚

更多细节,请参阅here. (The top two comments (this and this)可能会进一步阐明)。