Helping you solve those tough coding problems!

Merge one or more Git commits into a different branch Merge one or more Git commits into a different branch

Published on Apr 1, 2019 by Jamie Munro

Problem


You have a commit that you want to move from one branch to the other. This is commonly required when there are additional commits in the branch where your commit is, so you are unable to merge the entire branch.

Solution





Git offers a command called: git cherry-pick. To use this command, in your command line tool of choice run: git cherry-pick [commit hash]. You will need to replace [commit hash] with your actual hash from Github.

Discussion


To use the git cherry-pick command, you need to first find the commit hash from Github (one or more of course). I commonly use Github's website to achieve this. For example, I have a Git repository that contains some of my Knockout.js examples (https://github.com/endyourif/Knockout.js-Examples/commits/master).

This page provides a list of my recent commits. Once you find your commit message you wish to merge to the different branch Git offers a copy hash button. Clicking this button copies the hash to your clipboard. E.g. c2e02a43d721113c08a7953e5878e21667a739e5.

With your hash ID in hand, it's time to perform the several steps to use the Git command. In this example, let's pretend my commit is in the development branch and I want to merge it to the master branch. I need to start by switching branches followed by the cherry pick command:


git checkout master
git cherry-pick c2e02a43d721113c08a7953e5878e21667a739e5


Repeat this for each commit you wish to merge to your other branch.

Tags: Github | git | cherry-pick

My Books
ASP.NET MVC 5 With Bootstrap and Knockout.js
Knockout.js Building Dynamic Client-Side Applications
20 Recipes for Programming MVC 3
20 Recipes for Programming PhoneGap
Rapid Application Development with CakePHP