Scenario#

You were working on a certain branch of a git repository, and you committed some changes to it. Then you realize, this particular commit should also go to another branch of the repository BUT you are not ready for a complete merge. Maybe this commit was meant for the other branch?

You want to merge this particular commit from the current branch to the other branch of your requirement.

Resolution#

Merging a specific commit from one branch to another is pretty easy: use the git cherry-pick command.

The syntax is: git cherry-pick <commit hash>. Here is how you go about doing it.

First make a note of the commit hash using the git reflog or git log command.

Then, switch to the branch where you'd like to merge the commit and run git cherry-pick with the commit hash, as shown in the example below.

$ git cherry-pick d4d8e7c

Now you will have merged the specific commit from one branch to the other.

References#

  1. git-cherry-pick