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

Viewed 445

I have a branch, testing, that contains tons of changes to files in a/, and also to files in b/.

I want to port all of the commits from testing onto my master branch. However, I want to only port the changes to a/. On the master branch, b/ is in .gitignore, and should not show up in the history.

I'd like to do this in a way that preserves commit messages, authorship, and ideally timestamps.

I believe git filter-branch, or its recommended replacement, git filter-repo, might be the right tools for this. However, they seem targeted at whole-repository operations, not porting between branches, so figuring out how to use them for my purpose brings me to StackOverflow.

1 Answers

It turns out this was pretty easy with git filter-repo: I just needed to use the --refs option.

I created a new branch, testing2, from testing:

git checkout testing
git checkout -b testing2

then ran

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

Then I merged the newly-rewritten testing2 on top of master:

git checkout master
git merge testing2
Related