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

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.

Published on Apr 1, 2019

Tags: Github | git | cherry-pick

Related Posts

Did you enjoy this article? If you did here are some more articles that I thought you will enjoy as they are very similar to the article that you just finished reading.

Tutorials

Learn how to code in HTML, CSS, JavaScript, Python, Ruby, PHP, Java, C#, SQL, and more.

No matter the programming language you're looking to learn, I've hopefully compiled an incredible set of tutorials for you to learn; whether you are beginner or an expert, there is something for everyone to learn. Each topic I go in-depth and provide many examples throughout. I can't wait for you to dig in and improve your skillset with any of the tutorials below.