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.
- Sandy pointed to a Federico blog post which formats the commit range as patches, changes branches, and re-applies them. @lyager also recommended this method.
- Federico himself suggested using rebase --onto.
- Garrett and Havoc recommended just merging the branch and then rebasing out the commits I didn’t want.
- Garrett also suggested gitk --all.
- On IRC, Peter suggested combining git rev-list and cherry-pick.
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.
-
Pingback from ~robcee/ – Inspector Milestone 0.5 Preview on 24 June 2010 at 10:18 am
Comments are now closed.

1 comment
Trackback link: http://joeshaw.org/2010/06/22/667/trackback