Skip to main content
X

Explore your training options in 10 minutes

Git Tutorials

Git Cherry Pick: A Step-By-Step Guide

Christina Kopecky - December 29, 2020


As beginning developers, we learn git simply through repetition. We learn quickly what git pull, git push, and git commit each means. As we transition to work on bigger projects and collaborate with teams, we start to learn more advanced git commands that will help keep our codebase’s version control history straight between the individuals who work on a project.

One such command is git cherry-pick . The git cherry-pick command is used when we want to take specific commits from one branch and attach them to the HEAD of another feature branch or the master branch.

What is git cherry-pick?

Think of cherry-picking as working on a group project. Each person has a specific section he or she needs to work. At the end, they will combine each section into one total project. You might take parts of a certain section and splice it to a part in another section so the project will flow better.

Get offers and scholarships from top coding schools illustration

Find Your Bootcamp Match

  • Career Karma matches you with top tech bootcamps
  • Access exclusive scholarships and prep courses










By continuing you agree to our Terms of Service and Privacy Policy , and you consent to receive offers and opportunities from Career Karma by telephone, text message, and email.

This is in essence what git cherry-pick is: we take a commit or multiple commits from one feature branch and attach it as a new commit to another branch. Let’s take a look at how it works:

This basic diagram represents two branches from a project that is tracked by git. The letters represent different commits made to the git repository on their respective branches. The dashes represent the history, from oldest to youngest. Try to imagine the structure of the two branches and how they work when following these steps:

Steps to complete git cherry-pick

Think of each git commit or letter (as illustrated above) as a cherry. Each cherry has a unique hash associated with it – if you’re not sure what a hash is, think of it as a fingerprint or unique identifier that’s associated with the commit. We’ll need that hash in order to pick that cherry and add it to another branch.

  1. git checkout <name of branch you’d like to grab commit from>
    Checkout to the branch you would like to pick your commit/cherry from.
  2. git reflog
    The git reference log, reflog , keeps track of recent actions made. Here is an example of a git reflog:
    % git reflog
    bf654bb (HEAD -> master, origin/master) HEAD@{0}: commit: last commit message made
    2394353 HEAD@{1}: commit: where head was 2 commit messages ago
    b4b51eb HEAD@{2}: commit: where head was 3 commit messages ago


    Your commit reference log, if working with a large team, may have more information, such as the date, time, and/or author who made the commit. Here, though, we see the commit hash, the branch where the commit was made, the action made, and the actual commit message.

    Once you know which commit you would like to add to the other branch, take note of the hash – the hash is the string of numbers and characters at the beginning of each line in this example.
  3. git checkout <name of branch you would like to add commit to>
    After you have made note of the hash, switch to the branch you would like to add the commit to.
  4. git cherry-pick [-x] <commit hash>
    Use the git cherry-pick command with the commit hash to add the commit to that branch’s working tree. Use the -x flag when you are cherry-picking from a public branch as this will append a line that remarks the original commit it was cherry-picked from.
    Let’s take another look at our diagram after the cherry-pick:

Let’s take another look at our diagram after the cherry-pick:

Our “cherry”, or commit hash, that we chose in this example is “C”. After following the steps outlined above, the selected commit, “C” in this instance, is added as a commit to the history of the second branch.

Final Notes

The last thing to do is to copy any notes over from the cherry-picked commit. Use git notes copy <cherry-picked commit hash> <new-commit-hash> to copy over any notes that were made in the original commit.

Also, be sure to resolve any merge conflicts that might arise as a result.
In this article, we took a look at the process it takes to use an advanced git command called  git cherry-pick. Use it when you need to take a commit from one branch and add it to another branch.

About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.

What's Next?

Christina Kopecky

About the author: Christina is an experienced technical writer, covering topics as diverse as Java, SQL, Python, and web development. She earned her Master of Music in flute performance from the University of Kansas and a bachelor's degree in music with minors in French and mass communication from Southeast Missouri State. Prior to joining the Career Karma team in June 2020, Christina was a teaching assistant, team lead, and section lead at Lambda School, where she led student groups, performed code and project reviews, and debugged problems for students. Christina's technical content is featured frequently in publications like Codecademy, Repl.it, and Educative.

Skip to main content