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

问题描述

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

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

我想以一种保留提交消息,作者身份以及理想的时间戳的方式来做到这一点。

我相信git filter-branch或其推荐的替代词git filter-repo可能是解决此问题的正确工具。但是,它们似乎针对整个存储库操作,而不是在分支之间移植,因此弄清楚如何将它们用于我的目的使我进入StackOverflow。

解决方法

事实证明,使用git filter-repo非常简单:我只需要使用--refs选项。

我从testing2创建了一个新分支testing

git checkout testing
git checkout -b testing2

然后跑

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

然后,我将新重写的testing2合并到master上:

git checkout master
git merge testing2