184

I have BranchA which is 113 commits ahead of BranchB.

But I only want the last 10 or so commits from BranchA merged into BranchB.

Is there a way to do this?

BuZZ-dEE
  • 3,875
  • 7
  • 48
  • 76
NullVoxPopuli
  • 51,415
  • 69
  • 184
  • 335
  • 6
    possible duplicate of [How to merge a specific commit in git](http://stackoverflow.com/questions/881092/how-to-merge-a-specific-commit-in-git) – chharvey Mar 25 '14 at 10:32

3 Answers3

268

The git cherry-pick <commit> command allows you to take a single commit (from whatever branch) and, essentially, rebase it in your working branch.

Chapter 5 of the Pro Git book explains it better than I can, complete with diagrams and such. (The chapter on Rebasing is also good reading.)

Lastly, there are some good comments on the cherry-picking vs merging vs rebasing in another SO question.

grane2212
  • 636
  • 1
  • 9
  • 29
ewall
  • 23,955
  • 14
  • 62
  • 83
  • 2
    Question: say commit `A` is branched off of `master` and you do some work on it, creating child commits `B` through `E`. Say `E` only has 1 line added from `D`. If you `git cherry-pick E` into `master`, does it apply *all* the changes from `A` through `E` into the `master` branch, or does it *only* apply the change from `D` to `E`, namely, it adds only that 1 line to `master`? If the case is the former, how do I achieve the latter? (aside from manually copying and pasting) – chharvey Mar 25 '14 at 10:44
  • 9
    git cherry-pick -n If you don't want the cherry pick auto-committed. – Ben Flynn Jun 19 '15 at 18:09
7

If BranchA has not been pushed to a remote then you can reorder the commits using rebase and then simply merge. It's preferable to use merge over rebase when possible because it doesn't create duplicate commits.

git checkout BranchA
git rebase -i HEAD~113
... reorder the commits so the 10 you want are first ...
git checkout BranchB
git merge [the 10th commit]
Seth Reno
  • 4,891
  • 4
  • 37
  • 42
7

SOURCE: https://git-scm.com/book/en/v2/Distributed-Git-Maintaining-a-Project#Integrating-Contributed-Work

The other way to move introduced work from one branch to another is to cherry-pick it. A cherry-pick in Git is like a rebase for a single commit. It takes the patch that was introduced in a commit and tries to reapply it on the branch you’re currently on. This is useful if you have a number of commits on a topic branch and you want to integrate only one of them, or if you only have one commit on a topic branch and you’d prefer to cherry-pick it rather than run rebase. For example, suppose you have a project that looks like this:

enter image description here

If you want to pull commit e43a6 into your master branch, you can run

$ git cherry-pick e43a6
Finished one cherry-pick.
[master]: created a0a41a9: "More friendly message when locking the index fails."
 3 files changed, 17 insertions(+), 3 deletions(-)

This pulls the same change introduced in e43a6, but you get a new commit SHA-1 value, because the date applied is different. Now your history looks like this:

enter image description here

Now you can remove your topic branch and drop the commits you didn’t want to pull in.

artamonovdev
  • 3,758
  • 1
  • 23
  • 32