What Does Cherry-Picking a Commit with Git Mean?

Better Stack Team
Updated on June 24, 2024

Cherry-picking a commit with Git refers to the act of selecting a specific commit from one branch and applying it onto another branch. This allows you to pick individual commits and apply them to a different branch, regardless of whether the branches have a common ancestor.

Here's how cherry-picking works:

  1. First, you need to identify the commit you want to cherry-pick. You'll typically do this by finding the commit hash using git log or a Git GUI tool.
  2. Switch to the branch where you want to apply the cherry-picked commit.

     
    git checkout <target-branch>
    
  3. Use the git cherry-pick command followed by the commit hash of the commit you want to cherry-pick.

     
    git cherry-pick <commit-hash>
    

    This command will apply the changes introduced by the specified commit onto the current branch. Git will create a new commit with the same changes as the original commit but with a new commit hash.

  4. If the cherry-picked commit introduces conflicts with the changes in the target branch, Git will pause the cherry-pick process and ask you to resolve the conflicts manually. Once you've resolved the conflicts, you can continue the cherry-pick process by running git cherry-pick --continue.

  5. After resolving conflicts (if any), Git will apply the commit and complete the cherry-pick process.

Cherry-picking is useful when you want to selectively apply specific changes from one branch to another, without merging the entire branch. It allows you to apply bug fixes, features, or other changes from one branch to another with precision.

Note:

  • Cherry-picking is generally safe for single commits, but it can lead to complications if you're cherry-picking multiple commits or if the commits depend on each other.
  • After cherry-picking, the new commit will have a different hash from the original commit, even though it contains the same changes.
  • Cherry-picking is a powerful tool, but it should be used judiciously to maintain a clear and understandable commit history.
Got an article suggestion? Let us know
Explore more
Git
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github