如何将提交从一个 Git 分支移植到另一个分支,但没有一些文件

How to port commits from one Git branch to another, but without some files

我有一个分支 testing,其中包含对 a/ 中文件以及 b/ 中文件的大量更改。

我想将 testing 中的所有提交移植到我的 master 分支上。但是,我只想将更改移植到 a/。在 master 分支上,b/.gitignore 中,不应出现在历史记录中。

我想以一种保留提交消息、作者身份和理想情况下的时间戳的方式来执行此操作。

我相信 git filter-branch, or its recommended replacement, git filter-repo,可能是解决此问题的正确工具。但是,它们似乎针对整个存储库操作,而不是分支之间的移植,因此弄清楚如何将它们用于我的目的让我想到了 Whosebug。

事实证明,使用 git filter-repo 这很容易:我只需要使用 --refs 选项。

我创建了一个新的 b运行ch,testing2,来自 testing:

git checkout testing
git checkout -b testing2

然后 运行

git filter-repo --invert-paths --path b/ --refs testing2

然后我将 newly-rewritten testing2 合并到 master 之上:

git checkout master
git merge testing2