Git: Merge a Specific Commit From One Branch to Another

While working on one of my side projects version controlled by Git, I needed to copy and merge a commit from say BranchB to BranchA. Scenarios like this is where git cherry-pick comes in handy.

A cherry-pick 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.

To merge a commit in branch B with SHA-1 checksum of 0afc917e754e03 to branch A;

  • If you are not already in branchA, checkout to the branch (git checkout branchA)
  • Run git cherry-pick 0afc917e754e03
  • If there is any conflict; fix it, stage the changes and commit.

Easy as pie.