Skip to content
David Sherrill edited this page Jun 11, 2015 · 1 revision

Cherry Picking A Commit from Master to a Branch

Occasionally, we take a snapshot of the code for a release, while development continues on the master branch. However, we might discover a bug that needs to be fixed in both the master branch and the snapshots. To find out which branches exist in the repository, you can ask git:

>>> git branch -r

Assume we've taken a snapshot called 4.0b1; there is a commit on the master branch that we want to apply to this release too. The first thing is to get onto the 4.0b1 branch. With newer (circa 1.7.3) versions of Git, you can just run (on a clean branch):

>>> git checkout origin 4.0b1

with older versions of Git, this will give an error: "error: pathspec '4.0b1' did not match any file(s) known to git." If this happens, you can use the alternative syntax:

>>> git checkout -b 4.0b1 origin/4.0b1

The latter will create a new local branch called 4.0b1 and pull the latest 4.0b1 version in from the main repository. Whichever mechanism you use, you should now be on a 4.0b1 branch. Now we want to find the SHA number for the commit on the master branch that we want to apply. Only the first few digits of the commit SHA are required (enough to make it unique among all commits in the repo). The best source for the SHA number for the commit is the GitHub Code/Commits tab (the first few digits of the SHA are shown next to every commit). Looking at the timeline, we find that the SHA for the commit we want to apply (from the master branch) is 8d4854. To apply that to the curent branch, we run:

>>> git cherry-pick 8d4854

Now we can simply run the tests, and push the patch back into the repository's 4.0b1 branch:

>>> git push origin 4.0b1