The git cherry-pick command: what it is and how to use it
With the "cherry-pick" command, Git allows you to integrate selected, individual commits from any branch into your current HEAD branch.
Contrast this with the way commit integration normally works in Git: when performing a Merge or Rebase, all commits from one branch are integrated.
Cherry-pick, on the other hand, allows you to select individual commits for integration. In this example, only C2 is integrated into the master branch, but not C4.
When should I use cherry-pick?
The short answer is: as rarely as possible. The reason why you should use cherry-pick rarely is that it easily creates "duplicate" commits: when you integrate a commit into your HEAD branch using cherry-pick, Git has to create a new commit with the exact same contents. It is, however, a completely new commit object with its own, new SHA identifier.
Whenever you can use a traditional Merge or Rebase to integrate, you should do so. Cherry-pick should be reserved for cases where this is not possible, e.g. when a Hotfix has to be created or when you want to save just one/few commits from an otherwise abandoned branch.
The Git Cheat Sheet
No need to remember all those commands and parameters: get our popular "Git Cheat Sheet" - for free!
How do I use the git cherry-pick command?
In its most basic form, you only need to provide the SHA identifier of the commit you want to integrate into your current HEAD branch:
$ git cherry-pick af02e0b
This way, the specified revision will directly be committed to your currently checked-out branch. If you would like to make some further modifications, you can also instruct Git to only add the commit's changes to your Working Copy - without directly committing them:
$ git cherry-pick af02e0b --no-commit
If you're using the Tower Git client, you can cherry-pick one or multiple commits simply via drag and drop:
You can perform many complex Git operations with a simple drag and drop action in Tower. Click here to learn more about this feature.
Learn More
- Check out the chapter Merging Changes in our free online book
- More frequently asked questions about Git & version control