Open In App

Git - Cherry Pick

Last Updated : 29 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Git cherry-pick in Git allows you to select a specific commit from one branch and apply it to another. Think of it as copying a single change without merging the entire branch.

When you use this command, Git recreates the selected commit on your current branch with the same changes but it generates a new commit ID.

git cherry-pick  <commit-hash>

Working of Git Cherry-Pick

Each Git commit is uniquely identified by a SHA-1 hash, generated from:

  • Commit content
  • Metadata (author, timestamp)
  • Parent commit(s)

Here’s what happens during cherry-pick:

  • Identify the Commit: Git locates the commit based on the provided hash.
  • Generate a Patch: Git calculates the differences (diff) introduced by the commit relative to its parent.
  • Apply the Patch: Git applies these changes to your current working directory.
  • Create a New Commit: After applying the changes, Git creates a new commit on your current branch with these changes. This new commit has a different hash due to its different parent commit.

This allows you to selectively transfer changes from one branch to another without merging the full history.

Cherry-Pick Vs Merge Vs Rebase

CommandDescription
git mergeBrings all commits from another branch.
git rebaseRewrites history by changing the base of commits.
git cherry-pickApplies only one specific commit to the current branch.

When to Use Git Cherry-Pick?

Suppose a developer fails to recognize which branch he is currently on, and by mistake, he commits to another branch instead of committing to the main branch. Now to fix it, he has to first run git show, then save the commit, check out the main branch, apply a patch there, and commit with the same commit message. But all this can be done automatically by using just one command i.e. cherry-pick.

Visual Explanation

In order to understand better refer to the below diagram as follows:

Before Cherry Pick
Before Cherry Pick

Before Git Cherry-Pick

Branch Structure:

  • The Main Branch has a linear sequence of commits (shown in green).
  • At some point, a new branch (called Another Branch) was created from the main branch.
  • The Another Branch has one extra commit (orange) that doesn't exist in the main branch.

Key Point:

  • The orange commit represents a change that is only present in the "Another Branch".
  • At this point, the Main Branch has no knowledge of this change.
After Cherry Pick
After Cherry Pick

After Git Cherry-Pick

What Changed:

  • The orange commit from Another Branch has been cherry-picked and applied to the Main Branch.
  • This is visually represented by the red arrow labeled "CherryPick".

Result:

  • The Main Branch now has a copy of that commit (same content, different commit hash), without pulling in the rest of the commits or history from the other branch.
  • Both branches now share that same feature or bug fix, but the rest of the branches remain independent.

What You Learn from These Images:

  • git cherry-pick is not a merge: It doesn’t bring all the history.
  • It is selective: Only the targeted commit is copied.
  • It's useful when you want to apply a fix/feature to another branch without integrating everything.

Step-by-Step Guide to Use Cherry-Pick

Here is a step-by-step guide how to cherry-pick a commit from one branch to another:

Step 1: Initialize a Git Project

Opening the git bash and creating a new project named sample and initializing the repo using the git init command.

 

Step 2: Create and Commit a File

Creating a '.txt'file using vi command to the project let's say an index file and add it to our sample project and make a commit and write a commit message before pressing the Enter.

Note: After running the vi <file_name> command , type :wq to save and quit the file. 

 

One can check your commit by git log command easily:

 

Step 3: Create Branches

Now assume we have 2 versions, so create 2 different branches by using the git branch command and move to a branch, let us say 2 by using git checkout command.

Note: One can simple view all the branches by running the git branch command as shown in the below diagram.

 

Step 4: Add a Feature File in branch2

Now suppose you want to work on some new feature, so creating and adding a new feature file lets say feature.txt using vi and add command respectively as shown below. Then commit your changes with a commit message.

 

One can check your commit by git log command as shown below:

It is clearly showing our first commit where our branch 1 is there and in branch 2 it has moved away farther ahead and we are currently working on our feature in branch 2

Step 5: Fix a Bug in branch2

Now suppose we found a bug in our feature and we came to know that this same bug is also present in our 1 branch. 

And now we are trying to fix some bug or issue as shown below by adding a fix.txt file let's suppose and add it to the current branch i.e. 2 and committing the required changes.

 

Checking our final commits:

 

Step 6: Cherry-Pick Bug Fix into branch1

Now,we have fixed the bug in branch 2, but we also need to add this fix to our branch 1 also, but we don't want to merge this branch 2 into our branch 1, because the work might still be going on the feature. 

Thus, in this scenario, we can cherry-pick this particular commit. To do so, just copy the hash value highlighted in the above diagram, then move to branch 1 by using checkout, and then use the command cherry-pick and paste the hash we just copied.

 

As seen clearly from the above, then we notice that previously we have only index.txt before doing cherry-picking, but now we have the fix.txt file also in our 1st branch.

Now if we try to check git log --oneline, we will be able to see that commit also came in the branch 1.


Commit hash: A commit hash is a unique identifier that is generated by Git for every commit. Each commit has its one commit hash.

Note: While using this command make sure you are on the branch you want to apply the commit.

Common Errors and Troubleshooting in Git Cherry-Pick

Cherry-picking can lead to various issues, especially when dealing with complex or diverged codebases. Understanding the root causes and how to handle them is crucial for effective Git usage.

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 in the cherry-picked commit conflict with existing changes in the current branch.

Solution:

  • Use git status to identify files with conflicts.
git status
  • Manually resolve the conflicts in each file.
  • Stage the resolved files with git add <file>.
git add <file>
  • Continue the process using:
git cherry-pick --continue

2. Cherry-Picking a Merge Commit

Error Message:

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

Cause:
You tried to cherry-pick a merge commit without specifying which parent to use.

Solution:
Use the -m option to tell Git which parent to base the cherry-pick on (usually -m 1 for the first parent):

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

3. Cherry-Pick in Progress

Error Message:

You have not concluded your cherry-pick (MERGE_HEAD exists).

Cause:
A previous cherry-pick operation was interrupted (due to conflict or manual abort).

Solution:

  • To continue:
    git cherry-pick --continue
  • To abort:
    git cherry-pick --abort

4. Detached HEAD Warning

Scenario:

Running cherry-pick in a detached HEAD state can apply changes without a branch reference.

Solution:

  • Always ensure you are on the correct branch:
    git checkout branch-name

Similar Reads