git cherry pick - range of commits and exclude some in between

carte blanche picture carte blanche · Mar 28, 2013 · Viewed 15.4k times · Source

I normally use the following git command to cherryppick a range of gerrits..no how do I exclude a couple gerrits in between.. can the below command be modified or is there one where we can pick a range of gerrits and exclude the ones we want..

git cherrypick fromgerritSHA1..togerritSHA1

Answer

twalberg picture twalberg · Mar 28, 2013

You can specify multiple ranges:

git cherry-pick A..B C..D E..F

or even specific commits:

git cherry-pick A B C D E F

If you have lots of commits you want to exclude, it might be easier to do something like this (sort of like a poor man's git rebase -i for git cherry-pick):

git log --pretty=oneline A..F | tac > tempfile.txt
< edit tempfile.txt to remove the commits you don't want >
git cherry-pick $(awk '{print $1}' tempfile.txt)

Edit: Added recommendation to tac the log, since git cherry-pick wants to see the commits in the opposite order from what git log produces (could also use git log --reverse ...).