cherry picking a range of commits

At work we use git, and I often want to cherry pick a series of commits from a development branch, but don’t want to merge the whole branch for whatever reason. I put out a call on Twitter for ideas, and got a handful of good ones back.

Update, 27 July 2010: git 1.7.2 released earlier this week, has support for passing a range of commits to git cherry-pick. See the release notes. For my pre-1.7.2 solution, read on.

All fine suggestions. I wanted something that I could easily transform into an alias since I’ve been doing this a lot lately, and I’d like it to scale to large numbers of commits. I ended up going with Peter’s suggestion, and behold! I present to you:

git apple-pick1

or, as a git alias:

apple-pick = !sh -c 'git rev-list --reverse "$@" | xargs -n1 git cherry-pick' -

Given a range of commits, it cherry picks them onto the current branch. The workflow:

$ git checkout my-branch
... hackety hack, do a bunch of commits ...
$ git checkout master
$ git apple-pick abc123^..my-branch
Finished one cherry-pick.
[master abc123] tweak some junk
1 files changed, 9 insertions(+), 0 deletions(-)
Finished one cherry-pick.
[master def456] did some awesome stuff
6 files changed, 30 insertions(+), 22 deletions(-)
Finished one cherry-pick.

Thanks for the help guys, and I hope you gitinistas out there find it useful. What other cool alises have you developed or found that help your daily workflow? Tweet them to me.

1 Federico thinks I should call it git transplant instead. He’s probably right, but mine’s cuter.

Comments are now closed.