• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
NameHero® Blog

NameHero® Blog

Web Hosting Tips & Resources From NameHero

  • Hosting
    • Web Hosting
    • WordPress Hosting
    • WooCommerce Hosting
    • Enterprise Hosting
  • VPS
    • VPS Hosting
    • Flex VPS
  • Reseller
  • Email
  • Gaming
  • Domains
  • Website Builder
  • Account
  • Blog Home
  • Categories
  • Authors

Using Git Cherry-Pick To Apply A Commit From Another Branch

CJ Saathoff

Published on: May 1, 2024

Categories: Linux Command Line 0

Git is a distributed version control system that is used to keep track of changes to a set of computer files. This useful tool has quite many sub-commands, in this post we will look into one of those sub-commands the cherry-pick command. The cherry-pick command allows you to pick a specific commit from another git branch and bring it into your current branch.

  • Understanding Git Cherry-Pick
    • Definition Of Cherry-Pick In Git
    • Purpose Of Cherry-picking A Commit
    • Considerations
  • Steps To Cherry-Pick A Commit From Another Branch
    • 1. Identify The Commit You Want To Cherry-pick
    • 2. Switch To The Target Branch
    • 3. Using The Cherry-pick Command
    • 4. Resolve Any Conflicts That Arise
    • 5. Commit The Changes
    • 6. Push The Changes
  • Conclusion
  • Additional Links

Understanding Git Cherry-Pick

Definition Of Cherry-Pick In Git

The cherry-pick sub-command in git is used to apply a single commit from one branch and apply it to the current branch in another.

Purpose Of Cherry-picking A Commit

There are many reasons you may want to cherry-pick some examples are bug fixes, feature isolation, and backporting to name a few.

Considerations

  • Be aware cherry-picking only moves one commit to the new branch at a time.
  • Be prepared to resolve merge conflicts when working with cherry picked commits.
  • Document your cherry picks clearly, including their origin.
  • Consider alternatives like rebasing or merging instead.

Steps To Cherry-Pick A Commit From Another Branch

Make your current directory contain a git repository.

If you need a list of the current branches available for that repository use git branch -r.

1. Identify The Commit You Want To Cherry-pick

First, you will need the commit hash that you want to cherry-pick.

To look at existing commits to gather a commit hash, the git log is likely the easiest below is an example:

git log source-branch
  • If you get an error due to the branch not being stored locally use git checkout source-branch and try again.
  • Once you have git log information you will need to scroll through till you find the desired commit.
  • Note the commit hash down. For reference, the commit hash is outlined in the example below.Example of commit hash in git log output

2. Switch To The Target Branch

Now to change to the other branch, the branch that will be the target for the cherry pick. To change to the target branch you want to use the git checkout command, see the example below:

git checkout target-branch 
  • Make sure to replace target-branch with the branch name you want to apply the commit to.

3. Using The Cherry-pick Command

Now has come the time for the cherry-pick command.

git cherry-pick commit-hash --no-commit
  • Replace commit-hash with the commit hash that we identified in step one.

4. Resolve Any Conflicts That Arise

If any conflicts come up you will be prompted to resolve them before continuing.

5. Commit The Changes

Once any conflicts are taken care of you will want to do a new commit using:

git commit
  • You will then be asked to enter your commit message

6. Push The Changes

After you commit the changes make sure you remember to push them to the remote repository. Assuming the remote repository is already configured in this the command would be:

git push

Conclusion

Git is unquestionably a powerful command-line tool. Git has many sub-commands that provide many functions. In this blog post, we discussed the cherry-pick sub-command its definition, purpose, as well as considerations. From there wrapped up to a step-by-step guide on how to use the command.

Additional Links

Looking for more information what not check out these links:

  • Other posts on Git
  • Git Offical Documentation
  • Git cherry-pick Documentation
CJ Saathoff

Embracing a lifelong passion for technology since childhood, CJ delved into the intricate workings of systems, captivated by the desire to understand the unknown. This innate curiosity led to his discovery of Linux, a revelation that resonated deeply. With more than 7 years of on the job experience, he’s honed his technical skills as a Geek and Senior Linux Systems Administrator.

Related Posts

How to Use Crontab Reboot to Schedule Tasks

The crontab reboot command allows you to schedule tasks to run after you reboot the system. Here's how to do it.

How to Delete Lines with Sed in Linux

Here's how to delete lines with sed in Linux. Learn about pattern matching, deleting multiple lines, common mistakes, and more.

How to Reset the Last Commit in Git

By resetting the last commit in git, you can undo the latest changes. This allows you to make some changes to and commit them again.

How To List Remote Branches In A Git Repository

Learn how to list remote branches in a Git repository and some of the best practices to use when doing so with our guide!

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow & Subscribe

Exclusive promos, content and more!


Most Popular Posts

NameHero’s Recommended WordPress Plugin and Theme Setup (2024)

WordPress Hosting vs. Web Hosting – What’s The Difference?

How To Increase The InnoDB Buffer Pool Size

How To Fix A Stuck All-in-One WP Migration Import

How To Add A Subdomain In Cloudflare

Top Categories

  • WordPress
  • WordPress Tutorials
  • Enterprise Hosting
  • WooCommerce
  • Web Hosting
  • Resellers
  • Website Security
  • Website Development
  • Website Performance
  • VPS Hosting
  • SEO Tips
  • Announcements
  • Domain Registration
NameHero

NameHero® proudly provides web hosting to over 40,000 customers with 99.9% uptime to over 750,000 websites.

  • Master Card
  • Visa
  • American Express
  • Discover
  • Paypal
Products
  • Web Hosting
  • VPS Hosting
  • Flex VPS Hosting
  • WordPress Hosting
  • WooCommerce Hosting
  • Reseller Hosting
  • Enterprise Hosting
  • Email Hosting
  • Game Hosting
  • Domains
  • Website Builder
Help & Support
  • NameHero Blog
  • NameHero Gaming Blog
  • Support
  • Help Center
  • Migrations
  • Affiliates
  • Gaming Affiliates
  • Call 1-855-984-6263
Company
  • About Us
  • Contact Sales
  • Reviews
  • Uptime
  • We're Hiring

Copyright © 2025 Name Hero, LLC. All rights reserved.
NameHero® is a registered trademark.

  • Privacy Policy
  • Terms of Use
  • Acceptable Use Policy
  • Payment Policy
  • DMCA