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.

Git Cherry Pick

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 First Example

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

git-cherry-pick

 You have to be in the Main branch (where you wants to add a new commit)

Now you can use the following command. 

git commands

git cherry-pick feature_branch~2

or you use git cherry pick with commit hash.

git commands

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 commands

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 commands

git log --oneline

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 commands

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 commands

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. 

Git Cherry Pick Definition

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. 

git commands


#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 commands

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 commands

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 commands

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 commands

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.

  • If you cherry-pick from a private branch.
  • Or you are planning to delete the branch after the cherry pick operation. 

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 - 

  • Author name (who wrote the code)
  • Timestamp
  • And comment

In such a case, how to find who uses the cherry-pick command?

It's Simple.

Use the following git command.

git commands

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. 

  • Use cherry pick based on the commit date (oldest commit to latest commit).
  • If You need to pick many cherry-pick commits, consider merge.

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.

  • For Linux, the easy and best tool is a meld
  • For Windows, vsDiffMerge.exe or WinMerge

And the flow will be like this.

git commands


#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 

  1. 1
    Go to Team Explorer
  2. 2
    Now Checkout the branch (Your destination branch).

Go to Source and select the commit

  1. 1
    Go to the branch holding the commit and select View History...
  2. 2
    Finally, 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.

VS and Git Cherry Pick

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 commands

$ 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 commands

$ 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 commands

$ 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 commands

$ git cherry-pick --abort

#5. In case you need multiple commits, you can use this option.

git commands

$ git cherry-pick A..B

If you are looking to pick every commit between two commits, just use two dots between commits.

git commands

$ 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 commands

$ 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 commands

$ 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 commands

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


Leave a Reply

Your email address will not be published. Required fields are marked

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Related Posts

Git in PowerShell: Get Productive Results (in 2021)

Git HEAD: The Definitive & Easy Guide (in 2021)

31 Advanced Git Tips (Boost your Productivity)