Mastering Git Cherry Pick
(Comprehensive and Practical Guide in 2023)
In this post, I'm going to break down EVERYTHING you need to know about Git cherry pick.
What is cherry pick in Git?
Why it's important.
Why you need it.
Git cherry pick best practices & more.
Let's dive right in.

Don’t have time to read the whole guide right now?
No problem. I've created a downloadable pdf of Git Cherry Pick.
Also it contains my favourite "Lighthouse technique".
by Rajeev Bera
Updated June 18, 2023
Contents
CHAPTER 1:
Git Cherry Pick Fundamentals
In this chapter, I'll address the question: "What is Git Cherry Pick?".
I will dive into the essence of this crucial Git command. And to its fundamental role in managing source code effectively.
You'll also discover why understanding and leveraging Git Cherry Pick is still essential.
So, let's dive in.

What is a Git Cherry Pick?
Git Cherry Pick is a powerful Git command.
It takes commits from one branch and applies them to another branch.
Picking a commit from one branch and applying it to another is known as cherry picking.

Git Cherry Pick Command (and its Syntax)
In essence, Git Cherry Pick is like a time machine.
It allows you to pick a commit from the past and apply it to your present code. It's like taking a puzzle piece from one box and fitting it into another.
It's powerful and precise.
The basic command for Git Cherry Pick is straightforward.

git cherry-pick <commit hash>
You type git cherry-pick followed by the commit hash. The commit hash is a unique identifier for each commit.
Here's an example:

git cherry-pick 1a2b3c4d
You might be wondering:
How you can use Git cherry-pick to take single or multiple commits.
That's what I'm going to cover in the next chapter.
Keep reading…
CHAPTER 2:
Basic Git Cherry Pick Operations
Before we jump into the step-by-step guide for cherry-pick operations, it's important to understand what a good (or not so good) cherry-pick is.
This understanding will help you use Git Cherry Pick in the best way possible. It can make your code better and your project smoother.
Also, it can make working with Git easier and more efficient.
So, are you ready? Let's dive in!

How You Can Use the Cherry Pick Command in Git?
It's one of the simple git commands to use.
Let's take our first git cherry pick example.
So You have two branches Main and Feature. And you need to cherry pick a commit (let's say commit B002 from the feature branch).

You have to be in the Main branch (where you wants to add a new commit)
Now you can use the following command.

git cherry-pick feature_branch~2
or you use git cherry pick with commit hash.

git cherry-pick <commit hash>
And after this git command, It will apply a particular commit to your current branch (Main git branch in this case).
How Do You Cherry Pick Multiple Commits in Git?
It's super easy to use the Cherry Pick command for multiple random commits in Git.

git cherry-pick <hash1> <hash 2>... <commit hash n>
A Git commit hash is 40 digits long.
You can use the first seven digits of Git hash, and that will be more than sufficient.
You can use the following git command to see the short hash.

git log --oneline

This command will show only 7 digits hash (instead of 40 digits)
And in case you need to pick continuous commits, it's also easy.
Let's say you want to pick all the commits from A to C. Here A is the oldest, then C.

git cherry-pick A^..C
It will pick all the commits from A to C (including A)
In case you need to exclude the first commit (Commit A in our case)

git cherry-pick A..C
The vital point is that A should be older than C.
If you use the wrong order, the Git cherry-pick command will fail.
CHAPTER 3:
Advanced Git Cherry Pick Techniques
Did you know that getting really good at Git Cherry Pick can make your coding super powerful?
But there's a catch:
Just knowing the simple Git commands won't make you a coding superstar. The real magic happens when you learn some special Cherry Pick tricks. These can help you code faster and smarter.
In this chapter, I'll reveal these awesome Git Cherry Pick techniques.
Let's go...

Can You Git Cherry Pick From Another Remote Repo?
You can cherry-pick from another repo.
There is one little extra step for this.
You need to add the other repository as remote and then fetch all of its changes. Now you can continue with the cherry pick command.
Here are all the steps to Git cherry-pick from another repository.

#Adding (as "other") the repo from you want to cherry-pick
git remote add other <url>
#Fetch their branches
git fetch other
#List all commits
$ git log other/main
#It's time to cherry-pick the commits you need need
git cherry-pick ABC
Here is the more one-line and straightforward solution.

git fetch <remote-url> <branch> && git cherry-pick SHA1
How to use Git Cherry Pick in your Local Repo or Folder?
And what if another repo is not on remote, only in another folder.
So technically, it's possible to cherry-pick from your local repo.

git --git-dir=../<your_local_repo>/.git
format-patch -k -1 --stdout <commit SHA> |
git am -3 -k
In simple words, Here git format-patch command creates a patch from your_local_repo's commit. It will use the commit SHA as specified.
And finally, this patch is piped to git am. Git am command will apply the patch locally.
In git am command -3 means trying the three-way merge if the patch fails to apply cleanly.
How to Cancel Cherry Pick in Git?
There may be different scenarios when you want to cancel a Git Cherry pick operation.
Scenario 1
You are trying to run git cherry-pick <commit hash> and encounter merge conflicts.
In this situation, your Git cherry-pick operation is not complete yet. So you can abort the process with a simple git command.

git cherry-pick --abort
With the abort option, you can cancel the process and return to the previous state.
Scenario 2
Your git cherry-pick operation is complete and can go back to the previous state now?
In this situation, your Git cherry-pick operation is complete.
Here cherry-pick is just another commit. So you can use the following git reset command to undo it.

git reset --hard HEAD^
Make sure you do not have any other local changes.
You can stash your changes before executing this command, as this command will reset the HEAD.
Git Cherry-Pick and Conflicts
While Cherry Picking, you might have conflicts.
Can you avoid it?
Possibly yes.
But what happens if you have a conflict?
You have to sort these conflicts before you continue.
You may need an excellent mergetool to resolve them.
And the flow will be like this.

#Start the Git cherry pick
$ git cherry-pick <Commit Sha>
#And after this conflict happens
$ git mergetool
# Use your fav. tool and sort the conflicts
$ git cherry-pick --continue
# Now you can continue with the git cherry-pick
How to Revert a Git Cherry Pick?
Whether you have other local changes or not can influence the approach you take:

Scenario 1: If you have other local changes
1. Stash your current changes: This ensures that your current work is not lost when resetting the commit. You can do this using the `git stash` command.
2. Reset the cherry pick: To undo the cherry pick, you can reset to the previous commit (`HEAD^` signifies the previous commit) using the `git reset` command like so: `git reset --hard HEAD^`
3. Reapply your stashed changes: Now, you can get your stashed changes back into your working directory using `git stash pop`. If you wish to keep the changes in the stash, you can use `git stash apply`.
Here are the commands in order:

git stash
git reset --hard HEAD^
git stash pop
Scenario 2: If you have no other local changes:
In this case, you don't need to worry about stashing. You can directly reset to the previous commit like so:

git reset --hard HEAD^
In both cases, be cautious when using `git reset --hard` as it permanently discards commits.
If you've pushed your changes to a remote repository, it's generally better to use `git revert` instead to avoid disrupting the work of others.
CHAPTER 4:
Git Cherry Pick Use Cases & Practices
Want to be a pro at Git operations? Well, you need to get the hang of Git Cherry Pick!
But the real questions are:
When should you use Git Cherry Pick? How can you use it right? And when can it be a bit tricky?
In this chapter, I'll answer all of these questions. I'll give you clear rules and Git tips for using Cherry Pick.
So, let's dive in and figure out how to make Git cherry-pick your best friend.
(Without getting into any sticky situations)

Is Git Cherry Pick Bad Practice?
Is Cherry Pick Bad?
Heck no.
It is a valuable tool if you know how and when to use it. Also, you can use the git best practices with Git Cherry pick command.
When You Should Use Git Cherry Pick?
Now you know what Git Cherry Pick is and it’s not a bad practice.
Here I will try to explain a few cases which help you to understand when you can use the Git cherry-pick.

Scenario 1:
Let's say you are working on a feature that might take a few more days. But in the same branch, you have fixed some bugs.
In this case, you can use Git cherry pick to copy the bug fix from a branch to your main branch and make a release.
Scenario 2:
In this case, let's say If you have accidentally committed something important to the wrong branch.
Solution?
Use the cherry-pick command.
You can use Git cherry-pick to copy the commit in the correct branch. And then use git reset to undo this commit from the wrong branch.
When it can be an issue with Git Cherry Pick
Git cherry-pick generates a new commit (new hash with no reference to the original commit).
And the content of your new commit (done with cherry-picking) will ultimately be the same as compared to the old.
So the problem is duplicate commits in two or more branches.
CHAPTER 5:
Understanding and Mitigating Risks
If you're using Git Cherry Pick, knowing how to do it safely is essential.
But you might wonder:
How can I use Git Cherry Pick without messing things up?
What are the best ways to use it?
And how can I tell who made a specific cherry-pick?
This chapter is all about answering these questions.
Ready to learn more? Let's jump right in!

Is Git Cherry Pick safe?
It is safe since the cherry-pick only accepts the modifications from the indicated commit.
Also, at any time, you can cancel the git cherry-pick operation and can go back to the previous state.
So Git Cherry Pick is a safe Git command.
Git Cherry Pick Best Practices
#1. There will be no problems if you cherry-picked commits and removed the original branch. The only issue is that you must retain both branches.
#2. It would be best if you can use the -x option with the cherry-pick. The -x option which will add "cherry-picked from commit <commit sha>".
You should not use the -x option in two cases.
As -x option message "cherry-picked from ...". will be worthless to the receiver.
Cherry-picking is a powerful command that can cause problems if used without a proper understanding of what can happen.
How to Know the Committer of a Cherry-Pick in Git?
Git cherry pick uses the information from the original git commit.
Like -
In such a case, how to find who uses the cherry-pick command?
It's Simple.
Use the following git command.

git log --format=full
When you execute the above command, you can see the author info picked from the original commit.
But, the committer is the one who uses cherry-picking.
CHAPTER 6:
Comparing Git Cherry Pick
Want to get better at using Git? Then, it's key to know how Git Cherry Pick is different from Merge and Rebase.
But you might ask:
How can I tell these commands apart? And when should I use each one for the best result?
This chapter will help you answer those questions. I'll look at how Git Cherry Pick, Merge, and Rebase are similar and different.
I will also talk about when it's best to use each one.
Let's dive in!

What is the difference between Git Cherry Pick and Merge?
Git merge and cherry-pick have their boundaries.
Git Cherry Pick
In cherry-pick, you can move one commit from a branch to another.
Also, the compelling case of cherry-picking, you can use it within the same branch.
How?
Let's say you have a branch feature, and you make a few commits and a file.
Now you don't need that file, so you delete it.
But you realize you deleted the wrong file.
Now what?
You can use the git cherry-pick command in the same branch and restore the file.
Git Merge
In the git merge, you can move one or more commits from a branch to another.
And it's the preferred way as it will keep the commit history neat and clean.
Git cherry-pick has its own use, and It's not to replace the git merge or git rebase.
If you use the git cherry-pick overly, it may be wrong.
You may end up with not only many duplicate commits but also a dirty repo history.
What is the difference between Git Cherry Pick and Rebase?
Git Rebase and Cherry Pick serve different purposes.
Git Cherry Pick
With cherry-pick, you have the power to move specific commits from one branch to another.
Interestingly, cherry-pick isn't just for different branches.
How so?
Imagine you're working on a branch called 'feature'. You've made several commits and, accidentally, you commit something you didn't mean to.
Oops.
No worries though. You can use the git cherry-pick command right in the same branch to undo that accidental commit.
Git Rebase
On the other hand, git rebase allows you to integrate changes from one branch into another. But it's different from a simple merge.
Why?
Rebase effectively rewrites the project history by creating new commits for each commit in the original branch. This makes for a much cleaner project history.
Although git cherry-pick is useful, it's not a replacement for git rebase or git merge.
CHAPTER 7:
Git Cherry Pick Toolkit
It's time to delve deeper into the intricacies of the Git Cherry Pick operation.
You might be wondering:
Which options should I know about? And can I Cherry Pick from within Visual Studio?
This chapter will unravel these mysteries. It's time to expand your Git toolkit.
Let's get started!

Git Cherry Pick Command with Important Options
Here, you will learn about possible options using the git cherry pick command more effectively.
#1. To cherry-pick a commit from another branch.

$ git cherry-pick <commit hash>
#2. When you use cherry-pick, it will use the same commit message. But you can change it with the -e (or using --edit) option.
It will help you with the git commit message.
If you use -e, then it will allow you to update the commit message (before you commit)

$ git cherry-pick -e <commit hash>
#3. At the time of merge conflict, you resolve them.
And if conflict happens because of the cherry pick, You have to resolve it.
And after resolving conflicts, you can use the continue option to proceed with your git cherry-pick command.

$ git cherry-pick --continue
#4. In case you have some merge conflict.
Or you think to cancel the current operation. In that case --abort will be super helpful.
With the abort option, you can cancel the process and return to the previous state.
Git cherry-pick command behaves precisely like merge.
If git can’t apply the changes (e.g. in case of conflicts), in that case git leaves you to resolve the conflicts.
Once you resolve it, you can proceed with the commit or you can abort the operation.

$ git cherry-pick --abort
#5. In case you need multiple commits, you can use this option.

$ git cherry-pick A..B
If you are looking to pick every commit between two commits, just use two dots between commits.

$ git cherry-pick <first commit hash>..<last commit hash>
The important point is that A should be older than B. And it will fail if you use the wrong order.
#6. By default, cherry pick commits changes without any further warning.
If you want to pick a change but do not want to commit, use -n option.

$ git cherry-pick -n <commit hash>
With the use of -n flag, It will only stage all the changes.
#7. When you cherry-pick a commit, it will add a line to the original commit message.
And that line reads something like "cherry-picked from commit ...".
It helps to identify the original commit.
This flag is super helpful, especially in the case of duplicate commits in two or more branches.

$ git cherry-pick -x <commit hash>
Here the x is one of my favorite options.
Bonus
Too many options?
You don't have to memorize all the options.

$ git cherry-pick --help
You can use the above git command in your terminal.
Git Cherry Pick From Visual Studio
Visual Studio is one of the popular DEV environment tools.
And many developers use Visual Studio to copy changes to a branch with the cherry pick.
And visually, it's super easy.
Here are the steps.
Prepare your destination branch
- 1Go to Team Explorer
- 2Now Checkout the branch (Your destination branch).
Go to Source and select the commit
- 1Go to the branch holding the commit and select View History...
- 2Finally, select your fav commit and Right-click > select cherry pick from the menu.
That's it!
Now you should see the commit in your destination branch.
And you can repeat this process for another commit that you need to copy.
This is how it looks cherry picking in visual studio.

Chapter Bonus:
Git Cherry Pick Lighthouse technique
Now, I'll show you a cool trick.
It will help you handle your Cherry Pick changes like a pro.
This is called the Lighthouse Technique.
It helps you guide your code changes like a lighthouse guides ships.
Want to learn how?
Keep reading...

Step 1: Assess the Codebase (Lighthouse Surveys)
Like a lighthouse scanning the sea, look at your code. You want to find important changes that you want to pick.
Step 2: Identify Significant Commits (Spotting Ships)
Find key changes.
These changes are like ships a lighthouse needs to guide. They could be bug fixes or feature additions.
Example: Commit `f7d5c` fixes a major bug. This is a ship you'll want to guide.
Step 3: Record the Commit Hashes (Marking the Ships)
Each key change has a unique name, just like a ship. These names are called hashes.
Write them down.
Step 4: Navigate to the Desired Branch (Choose the Dock)
Decide where the key changes should go, like choosing a dock for a ship. This place is a branch in your code.
Example: Use `git checkout feature-branch` to navigate to the `feature-branch` dock.
Step 5: Apply the Cherry Pick (Guide the Ships)
Guide the ships into the dock using `git cherry-pick`.
Example: `git cherry-pick f7d5c` guides the bug fix from `f7d5c` into your `feature-branch` dock.
Step 6: Resolve Any Conflicts (Avoid Ship Collisions)
Sometimes, guiding ships can cause potential collisions or conflicts in code.
Make sure to resolve these conflicts to keep your codebase shipshape!
Step 7: Commit and Push (Dock the Ships)
After guiding your ships and avoiding collisions, dock your ships by saving your work with a commit. Then, share your work by pushing to the repository.
Example: `git commit -m "Guided bug fix"` docks your ship. `git push` shares it with others.
And that's it!
With the Lighthouse Technique, you guide the best of your code to the right place, like a lighthouse guiding ships at sea.
Now It’s Your Turn
Now I’d like to hear from you.
What’s your experience with Git cherry pick?
Or maybe you have a question which is not covered in this post.
Either way, please let me know by leaving a comment.