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

GitGit Cherry-PickGit Squash

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

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.

Solution 2 - Git

Useful alternative vs git cherry-pick -n for some use cases might be git merge --squash - for example, when you want to test a feature branch's changes on top of your integration branch, without rebasing.

Source: https://stackoverflow.com/questions/28189707/whats-the-difference-between-git-merge-squash-and-git-cherry-pick/28193740

Solution 3 - Git

Follow from master → cherry pick two commit from another branch

git checkout master
git cherry-pick :1  
git cherry-pick :2
git reset --soft HEAD~2 (number of cherry pick commits, i.e 2 ) 
git add . 
git commit

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondonquixoteView Question on Stackoverflow
Solution 1 - GitDavid DeutschView Answer on Stackoverflow
Solution 2 - GitlkraavView Answer on Stackoverflow
Solution 3 - GitJayen ChondigaraView Answer on Stackoverflow