Key Takeaways:
- Git cherry-picking lets you apply specific commits from one branch onto another.
- You find commit hashes with
git log <branch> --oneline
.
- Run
git cherry-pick <hash>
on the branch you want to add the commit to.
- For cherry-picking multiple commits, list their hashes in order.
- Fix conflicts manually if Git stops cherry-picking.
- Use
git cherry-pick --abort
to cancel if needed.
- Handle local changes with commits or stash before cherry-picking.
Git cherry-pick is a powerful command that lets you select specific commits from one branch and apply them to another. Unlike merging or rebasing, cherry-picking allows precise control over which changes to bring across, perfect for bug fixes or selective feature transfers. In this guide, you’ll learn what the git cherry-pick command is, how to use the git cherry-pick command, avoid common pitfalls, and leverage it for efficient version control. Plus, what is happening to the Git tree under the hood while all of the commands are being issued?
Table of Contents
What is Git cherry-pick Command?
“cherry-picking means choosing a commit from one branch and applying it to another”
To be more exact, cherry-pick in git applies the changes introduced by one or more existing commits onto another branch. Now, cherry-pick is similar to Git merge and Git rebase. But rather than taking an entire branch having commits or sort of moving commits around like you would do with merge or rebase, git cherry-pick allows you to apply only the changes that you want to apply. All right, now let’s talk about cherry-picking in practice and the actual commands needed to get it done.
Git Cherry-Pick Command Syntax
git cherry-pick <commit-hash>
Parameters:
<commit-hash> – The SHA-1 identifier of the commit you want to apply to your current branch.
Note: The Git cherry-pick commit ID is used to choose the commit that you want to commit.
When and Why to Use Git Cherry-Pick?
You should use git cherry-pick when you want to apply specific commits from one branch into another without merging the entire branch. It’s perfect for situations where you need just one or a few changes, not all the work from a feature or bug-fix branch.
Common Use Cases:
- Hotfixes in Production
Apply a critical bug fix from the development branch directly to the production branch without merging unrelated commits.
- Selective Feature Adoption
If a commit adds a useful feature but the rest of the branch isn’t ready, cherry-pick just that commit.
- Backporting Changes
Move a commit from the main branch to an older release branch that still needs updates.
- Recovering Missed Changes
If you accidentally committed to the wrong branch, cherry-pick the commit to the correct one.
Why Use Git Cherry-Pick?
- Precision: Apply only the changes you need.
- Avoid Merge Clutter: No unnecessary commits or merge history.
- Better Control: Keeps your Git history clean and relevant.
- Flexibility: Works for both single and multiple commits.
Common Git Cherry-Pick Commands and Flags
git cherry-pick has a few useful options that help handle different scenarios:
Command |
Purpose |
git cherry-pick <commit-hash> |
Apply a single commit to the current branch. |
git cherry-pick <commit1> <commit2> |
Apply multiple commits in one go. |
git cherry-pick <hashA>^..<hashB> |
Apply a range of commits from hashA (inclusive) to hashB (inclusive). |
git cherry-pick -n <commit-hash> |
Apply changes without automatically creating a commit (good for batching). |
git cherry-pick -e <commit-hash> |
Edit the commit message before committing. |
git cherry-pick --signoff <commit-hash> |
Add a “Signed-off-by” line to the commit message. |
git cherry-pick --abort |
Cancel an in-progress cherry-pick due to conflicts or mistakes. |
git cherry-pick --continue |
Resume cherry-picking after resolving conflicts. |
git cherry-pick --quit |
Stop cherry-picking without applying changes and without resetting the conflict resolution state. |
Step-by-Step to use Cherry Pick for Single commit
Let’s say you’re working on a web application with a co-worker. The main branch is the white line, and your co-worker has created a blue branch to work on navigation.
Before starting the navigation, they fixed a bug on their branch. Now, you’re working on the main branch and want to bring over just that bug fix, without including any unfinished changes. This is where git cherry-pick comes in and is the right tool. So, you are using git cherry-pick commit from another branch.
To use cherry-pick, you need to refer to commits by their commit hash, or in other words, the unique ID of the commit that you want to git cherry-pick. Now, to get those actual unique identifiers, run git log. Now, if you just run a regular git log, it’s just going to show you the commits from the branch you are on.
Step 1: Find the commit hash
To cherry-pick a commit, you need its unique identifier, called the commit hash.
- Run git log to see commits on your current branch.
- Run this command to see the commits on the nav branch (co-worker’s branch):
git log nav --oneline
- This will show commits from the nav branch with short hashes and messages.
- Find the commit that fixed the bug and copy its hash to cherry-pick.
- On the left side of the git log output, you’ll see each commit’s unique hash.
Step 2: Check out the target branch you want to apply the commit to.
- Make sure you are on the branch where you want to add the commit (in this case, main):
git checkout main
- You can confirm your current branch by running:
git status

Step 3: Run git cherry-pick
- Git will apply the changes from that commit and create a new commit on main with all those changes.
Step 4: Verify the new commit
Run:
git log --oneline
You will see the new commit on top with a new hash (different from the original nav branch commit).
But wait, what about multiple commits? Well, GitHub cherry-pick can do that too.
How to Make Multiple Commits Using Cherry-Pick
Suppose you, as a developer, want not only the bug fix but also a new logo added by their co-worker.
- Now, if you wanted to grab both of those without taking any of those other in-progress changes, you can do that with cherry-pick as well. So, the process will be similar.
- So to cherry-pick multiple commits, we’re just going to need the unique identifier of each of those commit hashes.
- So, once again, run git log, and figure out the hashes from the nav branch. Now, as you can see here, the logo is in this 384c452 commit, and then the bug fix is in 3a3bd61.
So we are just going to grab both of those hash codes, run cherry-pick git command, and then grab those two hashes and enter.
How Does It Work?
When you run git cherry-pick, Git applies the changes introduced by the selected commit(s) one at a time. Each applied change becomes a new commit on your current branch with a new hash.
What’s going on under the hood is git is applying the changes introduced by those commits one at a time in the order that they were specified in the cherry-pick command in git. So in this case, we have added the hash of the logo change first, followed by the hash of the bug fix commit second. So git is going to apply those in that order.
And at the end of the day, we’re going to end up with actually two new commits that contain those changes. If the changes introduced in those commits that were cherry-picked sort of have conflicts with what you already have on mainline, or if you were already working on those files that were also touched by that bug fix commit, you’re going to have a merge conflict.
Handling Merge Conflicts in Git Cherry-Pick Commits
Let’s learn how to solve merge conflicts that come up when using the git cherry-pick commit command. Now there are two main types of merge conflicts that typically come up when you’re using cherry-pick, and we’ll explain both.
Let’s discuss the first one. Here repository has the white branch as mainline, and let’s say your co-worker split off onto the blue branch to work on some navigational components.
But as you can see, they’ve also fixed a bug before they started working on that navigation component. Let’s say we want to take just that bug fix and pull it over into mainline without taking any of their other in progress changes. This is what git cherry-pick is really good for. Let’s understand it with a git cherry-pick example.
Using Git Cherry Pick to Resolve Merge Conflicts: Example
Step 1: Start Git cherry-pick and Detect Conflict
- Run git cherry-pick with the bug fix commit hash.
- Git may stop with a merge conflict if both branches modified the same file (e.g., config.yaml).

- The error message alone isn’t detailed, so run:
git status

This shows you:
- You’re on main and mid-cherry-pick.
- Which files have conflicts (e.g., config.yaml).
- A little cheat sheet about how to proceed in the case.
Step 2: Understand the Conflict
- Both the bug fix commit and a commit on main modified the same file config.yaml, causing a conflict.
- You can view the commit history (git log) to confirm this.


Step 3: Aborting a Cherry-Pick If Needed
If you want to cancel cherry-pick or eject from the situation, run git cherry-pick abort like this:
git cherry-pick --abort
- This resets everything back to normal.

Step 4: Manually Fix the Conflict
- Edit the file to keep the desired changes and remove the markers.
- For example, keep the bug fix changing show drafts from true to false, and also keep new social media handles added on the main.
Step 5: Stage and Continue cherry-pick
- Git will open an editor to update the commit message.
- Edit the message if needed, then save and exit.

Step 6: Verify the Commit
Run:
git log --oneline
- You’ll see the new cherry-picked commit on top with your conflict resolution.
Now, if I clear the screen and run git log one line, you can see I end up with that cherry-picked commit right on top that includes our merge.
Now, let’s understand the second type of merge conflict we get while cherry picking.
Additional Git cherry-pick Conflict Type: Local Uncommitted Changes
The second type of conflict is going to happen when those files on main aren’t committed yet, so in other words, they’re just your local changes.
- If you have uncommitted local changes that conflict with the cherry-pick, Git will abort the operation and show a message.
- To fix:
- Either commit your local changes first, or
Stash them using:
git stash
git cherry-pick
git stash pop
But when you go to pull those changes out of your stash you’re going to get a merge conflict that you have to solve, so either way, you’re on a collision course with merge. Now you can read fixing a git stash apply merge conflict is very similar to what you just learned in this blog, so you can easily figure it out.
Note: When popping the stash, conflicts may occur that you will have to resolve manually, similar to the steps above.
Now, there are times when you may think to use git rebase or git merge in place of git cherry pick, so let’s see how they differ from each other.
Git Cherry-Pick vs Git Merge vs Git Rebase
While all three commands deal with integrating changes, they work differently:
Feature |
Cherry-Pick |
Merge |
Rebase |
Purpose |
Apply specific commits from another branch |
Combine the entire branch history into another |
Reapply commits from one branch on top of another |
Scope |
Single or selected commits |
Full branch |
Entire commit history |
Commit History |
Adds a new commit using the git cherry-pick command with the commit ID for the copied changes |
Preserves both branch histories |
Rewrites commit history |
Common Use Case |
Hotfix from production branch to release branch |
Merging completed feature into main |
Keeping the feature branch up-to-date with the latest main |
Pros |
Fine control over which changes are applied |
Maintains full branch context |
Clean, linear history |
Cons |
Can lead to duplicate commits if not careful |
Can clutter history with merge commits |
Risk of rewriting history if misused |
Now, let’s understand how to do cherry-pick in git!
Best Practices for Using Git Cherry-Pick
- Verify the commit hash before running the command to avoid applying the wrong changes.
- Use -n if you plan to batch multiple commits before committing.
- Avoid cherry-picking git cherry-pick merge commits unless you know exactly how they affect the target branch; they often cause complex conflicts.
- Resolve conflicts carefully, review each change to avoid overwriting critical updates.
- Document why you cherry-picked in the commit message for future reference.
- Keep your branches updated before cherry-picking to minimize merge conflicts.
- Use it sparingly, as if you find yourself cherry-picking too often, it might be a sign that your workflow needs adjusting.
Troubleshooting Common Errors in Git Cherry Picking
When using git cherry-pick, you might encounter some common errors. These errors can arise due to conflicts, merge issues, or other git-related problems. Here’s a guide to troubleshoot some of the most frequent problems:
1. Conflict During Cherry-pick
This is the most common error, where git can’t apply the changes from the commit to the current branch due to conflicting changes.
Solution:
Check the conflict:
Run git status to see which files are in conflict. Git will mark these files with conflict markers (<<<<<<<, =======, >>>>>>>).
Resolve the conflict:
Open the conflicting files and manually resolve the conflicts. After that, stage the resolved files using:
git add <file>
Continue the cherry-pick:
Once you’ve resolved all conflicts, continue the cherry-pick by running:
git cherry-pick --continue
If you want to abort the cherry-pick (if it becomes too complicated), run:
git cherry-pick --abort
2. Cherry-pick Results in Unwanted Changes
Sometimes, after a cherry-pick, you may notice changes that weren’t in the original commit (e.g., line endings, whitespace changes).
Solution:
3. Commit Doesn’t Apply Due to Parent Commit Changes
If the commit you are trying to cherry-pick depends on changes that were made in previous commits, you might encounter issues.
Solution:
- Identify missing parent commits:
Use git log or git show to check the commit history. If the cherry-picked commit depends on changes from earlier commits, you may need to apply those first.
- Try rebasing or re-cherry-picking the right range of commits:
You may need to adjust which commits you are cherry-picking to make sure the necessary dependencies are also included.
4. “Already Applied” Error
Git may throw an error that the commit has already been applied if you’ve cherry-picked it before or if it exists in the branch’s history.
Solution:
- Check for existing commits: Run git log to see if the commit already exists in the branch history. If it does, you may not need to cherry-pick it again.
- Skip the cherry-pick: If you’re sure you don’t need the commit, you can skip it:
git cherry-pick --skip
5. Cherry-picking Across Diverged Branches
Sometimes, cherry-picking between branches that have diverged significantly can lead to unexpected results.
Solution:
- Rebase or merge before cherry-picking: If the branches have diverged heavily, it may be better to first merge or rebase the branches before performing the cherry-pick to minimize conflicts.
6. Incorrect Commit Hash
If you mistakenly type the wrong commit hash or attempt to cherry-pick a non-existent commit, you’ll receive an error indicating that the commit hash is not found.
Solution:
7. Detached HEAD State
When cherry-picking a commit, you might end up in a detached HEAD state, meaning you’re not on a branch anymore.
Solution:
8. Large Commits or Changes Fail to Apply
If a cherry-pick involves a large number of changes or a commit with a complex history, it might fail to apply.
Solution:
- Break up large commits: Consider splitting the commit into smaller, more manageable parts and then cherry-picking those individually.
- Reapply changes manually: If git can’t apply the changes, manually copy the changes and commit them.
9. “Patch Does Not Apply” Error
Sometimes, a cherry-pick fails with a message like “patch does not apply.” This occurs when the context for the commit (i.e., the changes being applied) has changed too much in the branch.
Solution:
- Examine the differences: Run git diff between the commit you want to cherry-pick and your current branch. Look for major changes that might be causing the issue.
- Apply changes manually: If the cherry-pick is not working due to context issues, you may need to apply the changes manually by copying over the relevant code.
Conclusion
Git cherry-pick is a powerful and flexible tool that lets you selectively apply specific commits from one branch to another without merging entire branches. It’s especially useful for applying bug fixes or isolated features quickly and precisely.
While it offers great control, cherry-picking can sometimes lead to merge conflicts, but with a clear understanding of how to resolve them, you can confidently manage your code changes. By mastering git cherry-pick, you enhance your version control workflow and keep your project history clean and focused.
Git Cherry-Pick FAQs
1. What is cherry-pick in git, and what does it do?
The git cherry-pick command allows you to select specific commits from one Git branch and apply those changes to another branch. Unlike merging or rebasing entire branches, cherry-pick lets you precisely control which changes to bring over. This is especially useful for applying bug fixes or features without merging unrelated commits, keeping your project history clean and focused.
2. How do I resolve conflicts in git cherry-pick?
When using git cherry-pick, conflicts may occur if the changes in the selected commit clash with existing changes on your current branch. To resolve conflicts, Git will pause and highlight conflicting files. You need to manually edit those files to fix the conflicts by choosing which changes to keep, then stage the resolved files using git add. Finally, continue the cherry-pick process with git cherry-pick --continue. If needed, you can abort the operation anytime using git cherry-pick --abort.
3. Can I cherry-pick multiple commits?
Yes, you can cherry-pick multiple commits in Git by specifying each commit hash separated by spaces, like git cherry-pick <hash1> <hash2>. Git applies these commits one by one onto your current branch in the order you list them. This method is great for selectively bringing over multiple fixes or features without merging entire branches.