The Wayback Machine - https://web.archive.org/web/20261203235909/https://www.geeksforgeeks.org/git/git-cherry-pick/

Git - Cherry Pick

Last Updated : 26 Nov, 2026

Git cherry-pick allows you to apply a specific commit from one branch onto another without merging the entire branch.

  • Selectively transfers changes between branches.
  • Helps apply bug fixes or important updates without integrating unrelated commits.
  • Creates a new commit on the target branch with the selected changes.

Working

Every Git commit is identified by a unique hash. Depending on the repository's object format, Git supports SHA-1 and SHA-256 commit hashes.

Here is what happens during cherry-picking:

  • Git locates the commit using its hash.
  • Git calculates the changes introduced by the selected commit relative to its parent.
  • Git applies those changes to the current branch.
  • Git creates a new commit containing the selected changes. Its hash is different because it has a different parent or commit metadata.

This allows developers to transfer selected changes between branches without integrating their complete histories.

Cherry-Pick vs Merge vs Rebase

Git provides different ways to integrate changes between branches. Cherry-pick, merge, and rebase differ in how they handle commits and branch history.

CommandDescription
git mergeIntegrates the histories of two branches. It may create a merge commit or perform a fast-forward update.
git rebaseReplays commits from one branch onto another base, creating new commits and rewriting the rebased branch's history.
git cherry-pickApplies the changes introduced by selected commits to the current branch, creating new commits without integrating the source branch's complete history.

Visual Explanation of Git Cherry-Pick

The following diagram shows how cherry-picking transfers a selected commit from a feature branch to the main branch.

Before Cherry-Pick:

BeforeCherryPick

After Cherry-Pick:

AfterCherryPick

Here, D' represents a new commit containing the changes introduced by commit D. The original commit remains on the feature branch, and the main branch receives a separate commit with the same changes.

When to Use Git Cherry-Pick

Cherry-picking is useful when developers need selected changes from another branch without integrating its entire history.

  • Applying Bug Fixes: Transfer a bug fix from a development branch to a stable branch.
  • Releasing Specific Features: Include selected commits in a release branch without merging ongoing development work.
  • Sharing Changes: Apply a useful commit across multiple branches.
  • Recovering Specific Work: Reapply a required commit without merging the original branch.

Step-by-Step Guide to Use Cherry-Pick

Let's create two branches, make changes in one branch, and cherry-pick a specific commit into the other branch.

Prerequisites

  • Git installed on the system.
  • Basic knowledge of Git commands and branches.
  • A terminal or Git Bash to execute commands.

Step 1: Initialize a Git Repository

Create a project directory and initialize a Git repository:

mkdir sample
cd sample
git init -b main

Create a file named index.txt and add some content:

echo "This is the main branch" > index.txt

Stage and commit the file:

git add index.txt
git commit -m "Initial commit"
git-init

Step 2: Create a Feature Branch

Create a new branch named feature and switch to it:

git switch -c feature

Create a file named feature.txt:

echo "New feature implementation" > feature.txt

Stage and commit the changes:

git add feature.txt
git commit -m "Add new feature"

Check the commit history:

git log --oneline
git-log---online

Step 3: Create a Bug Fix Commit

Suppose a bug needs to be fixed in the feature branch. Create a file named fix.txt to represent the bug fix:

echo "Bug fix applied" > fix.txt

Stage and commit the changes:

git add fix.txt
git commit -m "Fix application bug"

Save the commit hash of this change so that it can be used later:

FIX_COMMIT=$(git rev-parse HEAD)
FIX-COMMIT

Step 4: Switch to the Main Branch

Switch back to the main branch:

git switch main

Check the files available in the current branch:

ls

The fix.txt and feature.txt files are not present because their commits belong to the feature branch.

Step 5: Cherry-Pick the Bug Fix

Apply the selected bug fix commit to the main branch:

git cherry-pick "$FIX_COMMIT"

Git applies the changes introduced by that commit and creates a new commit on the main branch.

Check the files again:

ls

The fix.txt file should now be available in the main branch, while the unrelated feature changes remain exclusive to the feature branch.

Step 6: Verify the Cherry-Picked Commit

Check the commit history:

git log --oneline

The main branch now contains the initial commit and the newly created cherry-picked commit.

To compare the histories of both branches, use:

git log --oneline --all --graph

This displays the commit history of all branches and shows how the cherry-picked commit exists separately from its original commit.

git-log---online

Note: Always ensure that you are on the branch where you want to apply the selected commit before running git cherry-pick.

Common Errors and Troubleshooting in Git Cherry-Pick

Cherry-picking may encounter issues when the selected changes conflict with the target branch or when the command is used incorrectly. The following are common errors and their solutions.

1. Merge Conflict

Error Message:

error: could not apply <commit-hash>
hint: after resolving the conflicts, mark the corrected paths with 'git add'

Cause:

This occurs when the changes introduced by the selected commit conflict with existing changes in the target branch.

Solution:

  • Check the files containing conflicts:
git status
  • Open the affected files and resolve the conflicts manually.
  • Stage the resolved files:
git add <file-name>
  • Continue the cherry-pick operation:
git cherry-pick --continue

To cancel the operation instead, use:

git cherry-pick --abort

2. Cherry-Picking a Merge Commit

Error Message:

error: commit <hash> is a merge but no -m option was given

Cause:

This occurs when you try to cherry-pick a merge commit without specifying its mainline parent.

Solution:

Use the -m option to specify which parent should be treated as the mainline:

git cherry-pick -m 1 <merge-commit-hash>

Here, -m 1 selects the first parent as the mainline. Choose the appropriate parent based on the changes you want to apply.

3. Cherry-Pick in Progress

Error Message:

error: Cherry-pick is not possible because you have unmerged files.

Cause:

A previous cherry-pick operation may have encountered conflicts and remains unfinished. Git records the selected commit using CHERRY_PICK_HEAD while the operation is in progress.

Solution:

  • Check the current operation status:
git status
  • Resolve conflicts and stage the corrected files:
git add <file-name>
  • Continue the operation:
git cherry-pick --continue
  • To cancel the unfinished operation:
git cherry-pick --abort

4. Detached HEAD State

Scenario:

Cherry-picking while in a detached HEAD state creates a commit that is not attached to a branch. The commit may become difficult to locate after switching to another branch.

Solution:

Switch to the intended target branch before cherry-picking:

git switch branch-name

Then apply the selected commit:

git cherry-pick <commit-hash>
Comment

Explore