Reputation: 705
Specifically, I've done a diff on a commit range to see what files have changed:
git diff COMMIT_X COMMIT_Y --name-only
I get results like this:
/src/foo.php
/src/lib/bar.php
/src/lib/baz.php
/src/meta/test.php
I want to take all the changes introduced in these commits, but only for a subset of the files, for instance, just:
/src/foo.php
/src/lib/baz.php
How can this be achieved? Ideally just replaying it on top of my current branch would be best.
Upvotes: 2
Views: 51
Reputation: 489828
You can't quite do that. However, given:
$ git diff COMMIT_X COMMIT_Y --name-only
printing a list of more than just two files and you only want to pick up the changes for two of them, you can start with:
$ git diff COMMIT_X COMMIT_Y -- src/foo.php src/lib/baz.php
which will in fact get you the changes for just those two files. You can pipe the result to git apply
:
$ git diff COMMIT_X COMMIT_Y -- src/foo.php src/lib/baz.php | git apply
to get the same changes made to the current work-tree (if they apply cleanly), after which you can add and commit as usual. (You will have to make up your own cherry-pick-like commit message.)
Upvotes: 2
Reputation: 1984
As far as I know, you can't cherry-pick a part of a commit. You either cherry-pick the whole commit or you don't.
However, if (as in your case) you know which files should have changed and which don't you could try applying the commits you want and then reverting all the files that shouldn't have changed to their earlier state.
See the accepted answer to Reset or revert a specific file to a specific revision using Git? for how you can checkout a file as it was at an earlier point in time.
Afterwards you could commit the current state of your workspace (ie, the reverted files).
I haven't tried this but it seems like it could work. However, you're abusing some parts of git in ways that they weren't intended. Better would have been to make smaller commits containing units of work that should/will always be applied together so as to avoid this problem in the future.
Upvotes: 1