I have the following repository layout:
- master branch (production)
- integration
- working
What I want to achieve is to cherry-pick a range of commits from the working branch and merge it into the integration branch. I'm pretty new to git and I can't figure out how to exactly do this (the cherry-picking of commit ranges in one operation, not the merging) without messing the repository up. Any pointers or thoughts on this? Thanks!
Best Answer
When it comes to a range of commits, cherry-picking
iswas not practical.As mentioned below by Keith Kim, Git 1.7.2+ introduced the ability to cherry-pick a range of commits (but you still need to be aware of the consequence of cherry-picking for future merge)
damian comments and warns us:
If you want to pick the range
B
throughD
(includingB
) that would beB^..D
(instead ofB..D
).See "Git create branch from range of previous commits?" as an illustration.
As Jubobs mentions in the comments:
Note: as of Git 2.9.x/2.10 (Q3 2016), you can cherry-pick a range of commit directly on an orphan branch (empty head): see "How to make existing branch an orphan in git".
Original answer (January 2010)
A
rebase --onto
would be better, where you replay the given range of commit on top of your integration branch, as Charles Bailey described here.(also, look for "Here is how you would transplant a topic branch based on one branch to another" in the git rebase man page, to see a practical example of
git rebase --onto
)If your current branch is integration:
That will replay everything between:
first_SHA-1_of_working_branch_range
(hence the~1
): the first commit you want to replayintegration
" (which points to the last commit you want to replay, from theworking
branch)to "
tmp
" (which points to whereintegration
was pointing before)If there is any conflict when one of those commits is replayed:
git rebase --continue
".git rebase --skip
"git rebase --abort
" (and put back theintegration
branch on thetmp
branch)After that
rebase --onto
,integration
will be back at the last commit of the integration branch (that is "tmp
" branch + all the replayed commits)With cherry-picking or
rebase --onto
, do not forget it has consequences on subsequent merges, as described here.A pure "
cherry-pick
" solution is discussed here, and would involve something like:But anyway, when you need to "replay" a range of commits, the word "replay" should push you to use the "
rebase
" feature of Git.