Cherry Picking: git cherry-pick

Git Tips & Commands

Learn how to cherry pick a commit from a different branch in Git and enhance your programming skills. Take a further step into your programming career with our comprehensive coding courses and bootcamps.

Key Insights

  • To cherry pick a commit from a different branch, the first step is to navigate to the Git repo folder using the terminal.
  • The second step is checking out the branch where you want to work by executing the 'git checkout master' command.
  • Each commit has a unique hash, you need to find this hash from either the commit history on GitHub or Bitbucket or by running the 'git log --oneline' command in your terminal.
  • Once you have the hash for the commit you want to cherry-pick, you can execute the 'git cherry-pick' command followed by the hash.
  • It is possible to cherry-pick multiple commits by listing multiple hashes in the order they were originally committed.
  • Beyond Git, there are a variety of coding courses and bootcamps available including web development, Python, data science, and web design courses.

Essential Git Commands

  • git status

    What's changed, staged, and untracked — your most-used command.

  • git diff

    See exactly what's different before staging.

  • git log

    Inspect commit history with --oneline or --graph for visualization.

  • git reset

    Undo staging or commits — careful with --hard.

Cherry Pick a Commit from a Different Branch

  1. In your terminal (Terminal, Git Bash, or Windows Command Prompt), navigate to the folder that is your Git repo.
  2. Check out the branch you want to work on (not the one with the commit). Enter this command (if needed, replaced master with your desired branch):
    • Git checkout master
  3. Each commit has a unique hash (which looks something like 2f5451f). You need to find the hash for the commit you want to cherry-pick. Here are two places you can see the hash for commits:
    • In the commit history on the GitHub or Bitbucket or website.
    • In your terminal (Terminal, Git Bash, or Windows Command Prompt) run the command Git log—oneline
  4. Once you know the hash for the good commit you want to cherry-pick, run the following command (replacing 2f5451f with your commit's hash):
    • Git cherry-pick 2f5451f
Cherry Pick Multiple Commits

You can cherry-pick multiple commits by listing multiple hashes in the order they were originally committed.

Git cherry-pick hash1 hash5 hash7

Go Beyond Git

We offer the best coding courses and bootcamps for students at all levels of experience. Classes are hands-on and students receive workbooks with step-by-step instructions through exercises with real-world applications. Check out our programming courses now:

Full-Stack Web Development Certificate: Live & Hands-on, In NYC or Online, 0% Financing, 1-on-1 Mentoring, Free Retake, Job Prep. Named a Top Bootcamp by Forbes, Fortune, & Time Out. Noble Desktop. Learn More.

Topics

Learn more in these courses

How to Learn Git

Master Git with hands-on training. Git is a free, open-source version control system that allows developers to track the changes they make to code.

Yelp Facebook LinkedIn YouTube Twitter Instagram