Cherry-pick and squash a range of commits into a subdirectory or subtree

donquixote picture donquixote · Feb 1, 2016 · Viewed 32.2k times · Source

How can I tell cherry-pick to pick range of commits and squash it?

Or in other words, apply the diff between two commits to the current state of the repository?

The following does not work (cherry-pick has no --squash option):

git cherry-pick --squash e064480..eab48b59c

Note: My use case is within a subtree scenario - before anyone starts arguing that I should not squash.

The following works, but then I have a range of separate commits. I can squash them manually with interactive rebase afterwards.

git cherry-pick -X subtree=vendor/package e064480..eab48b59c

Is there any way to do the squashing as part of the cherry-pick?

Answer

David Deutsch picture David Deutsch · Feb 1, 2016

Pass -n to git cherry-pick. This will apply all the commits, but not commit them. Then simply do git commit to commit all the changes in a single commit.