Git is awesome at merging and branching, but what if you want to pull in just one patch from a single commit?
We ran into this recently with Insoshi at GitHub, where piotrj updated the README to be in RDoc format. Why not just merge in his changes? Well, Piotr has also been working on image galleries, but in the mean time billsaysthis has picked up that torch and run with it. As a result, Piotr’s image gallery changes would cause conflicts with the current master branch, and in any case we don’t want those changes just yet—we only want the RDoc-ified README for now. If only there were a way to use Git to cherry-pick just one commit…
Aha, git cherry-pick to the rescue! Here are the steps for my particular case:
- I use
$ git fetch piotrj
to fetch Piotr’s changes to my local machine. (I had already connected to his GitHub fork using the steps from the relevant Insoshi Git guide.)
- Looking at GitHub for the commit label, I see that it’s 3b4257f0454fc31349a0505c9a883f691fe8889d. (I could also checkout Piotr’s branch locally and use git log to see the commits.) So all I need to do is switch to the master branch and cherry-pick the change:
$ git checkout master $ git cherry-pick 3b4257f0454fc31349a0505c9a883f691fe8889d
Note that I don’t have to reference Piotr’s branch explicitly; Git figures out the right branch to use from the commit label.
That’s it! Amazingly, I don’t even have to do a commit; Git adds Piotr’s message to my log automatically. After pushing the updated master branch to GitHub, the Insoshi README is noticeably improved. Thanks to piotrj—and to git cherry-pick!