Skip to content

Git Cherry-Pick Tutorial

Overview

Git cherry-pick allows you to apply changes from a specific commit onto your current branch. This is useful when you want to introduce specific changes from one branch into another without merging the entire branch. This document covers the basics of using git cherry-pick with examples.

What is Git Cherry-Pick?

Git cherry-pick allows you to select specific commits from one branch and apply them to another branch. This can be useful for hotfixes or for applying a particular feature to multiple branches without merging the entire branch.

Basic Cherry-Pick Usage

To cherry-pick a commit, you need the commit hash of the commit you want to apply to your current branch.

Example

  1. Check out the branch you want to apply the commit to:

    git checkout main
    
  2. Use the cherry-pick command with the commit hash:

    git cherry-pick <commit-hash>
    

Example

git cherry-pick abc1234

This will apply the changes from commit abc1234 to the main branch.

Cherry-Picking Multiple Commits

You can cherry-pick multiple commits by providing a range of commits or a list of commit hashes.

Example: Cherry-Picking a Range of Commits

  1. Check out the branch you want to apply the commits to:

    git checkout main
    
  2. Use the cherry-pick command with a commit range:

    git cherry-pick <start-commit-hash>^..<end-commit-hash>
    

Example

git cherry-pick abc1234^..def5678

This will apply the changes from all commits between abc1234 and def5678 (inclusive) to the main branch.

Example: Cherry-Picking a List of Commits

  1. Check out the branch you want to apply the commits to:

    git checkout main
    
  2. Use the cherry-pick command with a list of commit hashes:

    git cherry-pick <commit-hash1> <commit-hash2> <commit-hash3>
    

Example

git cherry-pick abc1234 def5678 ghi9101

This will apply the changes from commits abc1234, def5678, and ghi9101 to the main branch.

Handling Conflicts During Cherry-Pick

Conflicts can arise during a cherry-pick if the changes in the commit(s) being applied conflict with changes in the current branch.

Example

  1. Cherry-pick a commit that results in a conflict:

    git cherry-pick abc1234
    
  2. Git will notify you of conflicts. Resolve the conflicts in the affected files.

  3. After resolving the conflicts, add the resolved files:

    git add <resolved-file>
    
  4. Continue the cherry-pick process:

    git cherry-pick --continue
    

Abort or Continue a Cherry-Pick

Abort a Cherry-Pick

To abort an ongoing cherry-pick operation:

git cherry-pick --abort

This will stop the cherry-pick process and revert any changes made by the cherry-pick.

Continue a Cherry-Pick

If you have resolved conflicts and want to continue the cherry-pick process:

git cherry-pick --continue

Useful Tips

  • Cherry-Pick from Other Branches: You can cherry-pick commits from any branch by specifying the branch name before the commit hash.

    git cherry-pick feature-branch abc1234
    
  • Cherry-Pick with a Message: You can edit the commit message during a cherry-pick by using the -x option.

    git cherry-pick -x abc1234
    

    This will add a reference to the original commit in the commit message.

Summary

Git cherry-pick is a powerful tool for applying specific commits from one branch to another. By understanding how to use cherry-pick for single or multiple commits, handle conflicts, and manage the process, you can effectively incorporate necessary changes into your branches without merging entire branches.