What Does Cherry-Picking a Git Commit Mean?

Find out what is meant by git cherry-picking a commit

Cherry-picking a git commit means to select a specific commit from any branch and applying it to another. For example, to cherry-pick a commit with the hash "3ba53ff050ef253058088eff5" to a branch called "foo", you would do the following:

# 1: switch to the target branch
git switch foo

# 2: cherry-pick commit and apply it to target branch
git cherry-pick 3ba53ff050ef253058088eff5

When you cherry-pick a git commit, the following is true:

  • The commit in the source branch is not removed;
  • A copy of the commit with the same changes and the same commit message is created in the target branch;
  • The history of the target branch is not altered, but rather added to, as a new, distinct commit is created.

Cherry-picking is useful in certain situations, even though it may not always be a best practice (as it can result in duplicate commits). For example, following are some cases in which cherry-picking is useful:

  • When you accidentally commit something to the wrong branch;
  • When developing features in parallel, where you may need to include a feature from another branch that another person might be developing;
  • When you may need to patch a critical bug on production site, while the feature development might still be ongoing;
  • When you may need a specific commit from a stale pull request (that was closed without being merged).

In most other cases, using traditional merges is usually the preferred way.

Hope you found this post useful. It was published . Please show your love and support by sharing this post.