blog.mhartl | Michael Hartl's tech blog


Using Git to pull in a patch from a single commit

Filed under: Git, Insoshi, Ruby on Rails — mhartl @ 13:06

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:

  1. 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.)

  2. 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!

About these ads


  1. Ok, this is great, but I have one question: How do I cherry-Pick from another machine on the other side of the world? Is there a “git cherry-pick ssh” option? I could not find one so far.

    Thank you for your Feedback.


    Comment by Zeno Davatz — 2008-08-12 @ 08:07

  2. You still have to pull in the remote commit information using

    # Add remote if you haven’t already
    git remote add [remote nickname] [remote URL]
    # Fetch branches and commits from the remote
    git fetch [remote nickname]

    If the remote machine doesn’t have remote read-only access to its repositories configured via git-daemon, gitosis, ssh or http, you’ll have to go through the process of pushing that remote machine’s changes up to somewhere that does (like GitHub). You’ll then use that place as the specified remote.

    Once you have the commit information has been saved locally, you can run

    git cherry-pick [commit]

    Comment by long — 2008-08-12 @ 17:39

  3. Thank you Sir! for this information! That would be great if one could add that to the man page (if you have any connections to the people who are writing it).

    I done exactly as advised and added the remote and then just done a pull and not a fetch. But if I wanted to be more cautious I could have done a “git fetch” and then a “git cherry pick”.

    A git-cherry-pick with SSH capabilities would also be very nice.


    Comment by Zeno Davatz — 2008-08-14 @ 00:40

  4. Courage from hearts and not from numbers grows….

    To win a race, the swiftness of a dart Availeth not without a timely start….

    Trackback by science stage — 2012-05-11 @ 13:32

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

Customized Shocking Blue Green Theme Blog at


Get every new post delivered to your Inbox.

Join 28 other followers

%d bloggers like this: