Git Cherry Pick
(The Definitive & Crystal Clear Guide)
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.

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.

How do I 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.
When You Should Use Git Cherry Pick?
Now you know what Git Cherry Pick is.
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.
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.
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
Can You Git Cherry Pick From 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.
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.
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.
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.
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
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.
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.

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.
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.