daniel-s
6/3/2016 - 1:46 PM

Git Advanced

Git Advanced

##Cherry-Picking specific commits from another branch https://ariejan.net/2010/06/10/cherry-picking-specific-commits-from-another-branch/

How to merge only specific commits from another branch into the current one:

dd2e86 - 946992 - 9143a9 - a6fd86 - 5a6057 [master]
           \
            76cada - 62ecb3 - b886a0 [feature]

We want to have commit 62ecb3 in the master branch, but not the other code in the feature branch:

git checkout master
git cherry-pick 62ecb3

Cherry-pick several commits

Now we want to have 76cada and 62ecb3 in the master branch.

First we create a new branch from feature at the last commit we want, in this case 62ecb3:

git checkout -b newbranch 62ecb3

Then we rebase that newbranch commit --onto master. The 76cada^ indicates that we want to start from that specific commit:

git rebase --onto master 76cada^

So the result will be (or similar):

dd2e86 - 946992 - 9143a9 - a6fd86 - 5a6057 - 76cada' - 62ecb3' [master]
           \
            76cada - 62ecb3 - b886a0 [feature]

A similar case would be http://stackoverflow.com/questions/2369426/how-to-move-certain-commits-to-another-branch-in-git:

From:

o-o-X (master HEAD)
     \
      q1a--q1b (quickfix1 HEAD)
              \
               q2a--q2b (quickfix2 HEAD)

to:

      q2a'--q2b' (new quickfix2 HEAD)
     /
o-o-X (master HEAD)
     \
      q1a--q1b (quickfix1 HEAD)

With:

# let's go to current master (X, where quickfix2 should begin)
 git checkout master

 # replay every commit *after* quickfix1 up to quickfix2 HEAD.
 git rebase --onto master quickfix1 quickfix2

It seems that from Git 1.7.2+ it's possible to cherry-pick a range of commits (http://stackoverflow.com/a/1994491/3149679):

git cherry-pick A..B

Note that cherry-pick A..B will not get commit A (you would need A~1..B for that), and if there are any conflicts git will not automatically continue like rebase does (at least as of 1.7.3.1)