Search code examples
gitgit-cherry-pick

How to cherry-pick a branch that contains merges


We develop new features in feature branches that branch off master. Sometimes, such features shall be back-ported to a previous version of the software, and we'd like that preferably with the history intact.

I understand that I can use git cherry-pick A..B to pick the range of commits. However, long lived feature branches might contain merges to keep them up to date with master. When encountering merge commits, cherry-pick aborts.

Is there some (preferably canonical) way to do so? Something like

git cherry-pick --ignore-merges master...feature

For reference, the branch setup looks somewhat like this. I'd like to cherry-pick the commits H, K and K from my feature branch, into version_old.

A - B - C - D - E - L [master]
|       \       \
|        H - J - M - K [feature]
\
 F - G [version_old]

Solution

  • git log --pretty=%H --reverse --no-merges A..B | git cherry-pick --stdin