Git Cherry Pick: Getting the Exact Commit You Want (2023)

You don’t always want to merge after you’re done working with a branch. Often, what you need is to get one or more specific commits rather than all the changes. That’s what the git cherry-pick command is for.

In this post, you’ll get a detailed guide to this command, complete with code samples and diagrams. We assume you’re comfortable using the command line and are familiar with the most common Git commands.

Git Cherry-Pick: The Fundamentals

Let’s begin with the fundamental questions.

What Is Git Cherry-Pick?

Like git merge, the git cherry-pick command is one you use to incorporate changes from other branches into your current branch. How do they differ, though?

Why Is Cherry-Pick Used?

When merging, you’ll typically want to incorporate all the changes made in the other branch into the current one.

But sometimes you want only some of the changes. In these cases, cherry-picking comes in handy. A classical use case for git cherry-pick would be to backport a bug fix to an older version of the application.

How Do I Use the Cherry-Pick Command in Git?

Having covered the “what” and “why” of the command, let’s proceed to the how. As you’ll see, the git cherry-pick command is powerful and flexible, offering many possibilities.

The easiest and most common way of using it is cherry-picking a single commit: git cherry-pick <COMMIT HASH>.

Let’s see an example:

mkdir demo

cd demo

git init

echo Hello world! > file.txt

git add .

git commit -m “Initial commit”

After executing the commands above, you’ll have a repository with a single commit. Visually, we could represent the state of the repository like this:

Now, some more commands:

git switch -c new

(Video) Git cherry pick tutorial. How to use git cherry-pick.

echo Goodbye world! > file2.txt

git add file2.txt

git commit -m “Add second file”

echo More text >> file.txt

git commit -am “Update file.txt”

The commands above do the following:

  • Create a new branch and switch to it

  • Add a new file and commit it

  • Add a new line to the original file and commit it

This is how this new branch looks:

Now, let’s say you want to get some changes from new into the original branch. You’re only interested in the change made to the original file. For that, you can use git cherry-pick.

Start by getting the hash of the commit you’re interested in, using git log --oneline:

In my case, the hash I want is 9e1429a. Your actual value will differ; write it down anyway.

Now, it’s only a matter of returning to the initial branch and cherry-picking the commit I want:

git switch main

git cherry-pick 9e1429a

I can now run git log --online again to verify I really have the new change:

As you can see, the change is now integrated into main with a commit with the same message as the original commit on new. The hash, however, is different, indicating that the commit isn’t the same.

(Video) Git Cherry-Picking: Handling urgent hotfixes | Git Workflows

That’s because Git figures out the change it needs to apply to cherry-pick the specified commit and then applies those changes as a new commit in the target branch.

This is the final state of the repository:

Git Cherry-Pick: More Examples

With the most basic usage example out of the way, let’s now see some more advanced ones.

Cherry-Picking Without Creating a Commit

As you’ve seen, in the typical form, git cherry-pick results in a new commit being created in the destination branch. But sometimes you don’t want that. You might want to add more changes before committing, for example.

Going back to our example, suppose you now want to get the other commit from new (the one that introduced the second file). However, for whatever reason, you don’t want to commit right away. For that, you’d use the --no-commit option (or the -n short version):

git cherry-pick 38e9025 --no-commit

Now, by running git status, you can see the change has been added to the stage but not yet committed:

You’re now free to add more changes before committing.

Cherry-Picking Using a Different Identificator for a Commit

You’re not restricted to using a hash to identify the commit you want to cherry-pick. Instead, you can use other references that “resolve” to a commit id, such as branch names or tags.

For instance, consider our first example. Since we wanted to bring exactly the last commit on new, we could’ve used git cherry-pick new instead, and it would’ve worked the same way.

Cherry-Picking Multiple Commits

You’re also not restricted to cherry-picking a single commit at a time. Instead, it’s possible to provide multiple commits, or even a range, as arguments for the command.

For instance, going back to our previous example, let’s say you wanted to cherry-pick the two last commits from new into main. You’d just have to run the following command:

git cherry-pick 8db0902 fd35fc1

The result of the command above would be two new commits with the same changes and content of the original commits from new.

You can go further and cherry-pick a range of commits, using the following syntax to replace A and B with actual identifiers for commits:

git cherry-pick A^..B

The command above assumes that A is an older commit than B. Also, it includes both extremities. What if you didn’t want to include A? In such case, you’d run this:

git cherry-pick A..B

(Video) How to cherry pick a commit from another branch in git ? || git cherry-pick interview question

Cherry-Picking Gone Wrong

The git cherry-pick command is a powerful one, but it has some pitfalls you must be aware of.

Cherry-Picking Conflicts

Cherry-picking works by figuring out the patch—that is, the changes—introduced by a given commit and then applying that patch to the current branch.

That might result in conflicts if the commit you decided to cherry-pick builds on changes introduced by an earlier commit you didn’t cherry-pick.

Let’s go back to our original example. Go to the branch new and add two more commits, editing the first file:

echo “Another line” >> file.txt

git commit -am “Add another line to file”

echo “Yet another line” >> file.txt

git commit -am “Add yet another line to file”

Here are the two commits outputted by git log --oneline:

Now, let’s say I try to cherry-pick only the last commit:

git switch main

git cherry-pick ec3c2e1

I get a conflict:

Git is unable to apply the change from the last commit in new since it builds upon the previous one. Showing how to fix a conflict would be out of the scope for this post. It’s virtually the same as fixing a regular merge conflict, and we have another post that shows that in detail.

In our case, I’ll simply cancel the cherry-pick operation by running git cherry-pick --abort.

Logical Conflicts

For this example, I prepared a repository beforehand. Start by cloning the repository. It’s a silly .NET console app that asks for a number and then displays the multiplication table for that number.

After cloning, access the folder and open it using Visual Studio Code or another text editor/IDE of your preference. This is what you should see:

Back at your terminal, execute git switch cleaner-code. With this, you’ll switch to the other branch in the repo. You’ll notice that your project now has a new file, Utils.cs. You’ll also notice that the Program class looks different.

(Video) Part 13: How to use git cherry-pick command to bring changes from one branch to another branch?

That’s why I extracted the logic necessary to calculate the multiplication table to a different file. However, I did that over two commits, as you can see in the image below:

Back to the main branch, let’s try to cherry-pick just the last commit. You can use the hash for that, but this time I’ll go with the branch name:

git switch main

git cherry-pick cleaner-code

The command itself works just fine, as evidenced by the output of running git log --oneline:

However, if I try to run the app, things don’t look so nice:

What happened? A logical conflict happened.

The last commit—the one I cherry-picked—uses the extracted function, which was created in the second commit. Git happily completes the cherry-pick operation since it doesn’t result in a conflict. But the build fails because I’m effectively trying to use a function that doesn’t exist on this branch.

Messy History

Often, you might want to merge from a branch after cherry-picking commits from it. That might result in “duplicated” commits in the destination branch.

For instance, continuing with our example, let’s say we now want to merge cleaner-code into main. You can just run:

git merge cleaner-code

But take a look at what our history looks like now:

As you can see, we now have two commits with the message “Add call to the extracted function.” One is the result of the cherry-picking operation, and the other is the original commit brought by the merge.

This makes the project’s history confusing and harder to navigate.

Git Cherry-Pick: Use This Double-Edged Sword Wisely

Git cherry-pick is a powerful command you can use to get specific changes from other branches. However, with great power comes great responsibility—if you don’t use it wisely, it can lead to issues.

As a rule of thumb, choose to merge or rebase most of the time, reserving git cherry-pick only for situations where it’s the only choice.

Also, beware of branching strategies that rely on long-lived feature branches. In limited scenarios, they might be useful, but for the most part, they add unnecessary complexity. By adopting feature flagging, you can adopt leaner workflows that accelerate your delivery of value to production.

This post was written by Carlos Schults. Carlos is a consultant and software engineer with experience in desktop, web and mobile development. Though his primary language is C#, he has experience with a number of languages and platforms. His main interests include automated testing, version control and code quality.

(Video) Git Cherry Pick Example [Intermediate Git Tutorial]


How do I select a specific commit? ›

Go to either the git log or the GitHub UI and grab the unique commit hashes for each of the commits that you want. "Cherry pick" the commits you want into this branch. Run this command: git cherry-pick super-long-hash-here . That will pull just this commit into your current branch.

Does cherry pick move the commit? ›

Cherry picking is the act of picking a commit from a branch and applying it to another. git cherry-pick can be useful for undoing changes. For example, say a commit is accidently made to the wrong branch. You can switch to the correct branch and cherry-pick the commit to where it should belong.

How do you get a specific commit from another branch? ›

Copy commit from one branch to another without using git merge
  1. While on the wrong branch (the one that has the commit), do git log and copy the commit hash.
  2. Checkout to the correct branch which you want to apply the commit, eg git checkout master.
  3. Now apply the commit to the new branch, git cherry-pick <commit-hash>
20 Nov 2020

Can we cherry pick pull request? ›

Cherry-picks all changes from the pull request's source branch to the new topic branch. Prompts you to create a new pull request to merge the new topic branch into another target branch.

Can we cherry pick two commits? ›

It is possible to cherry pick multiple commits using the command line. Git 1.7. 2 introduced the ability to cherry pick a range of commits.

How do I switch to a specific commit in git? ›

Go back to the selected commit on your local environment

Use git checkout & the ID (in the same way you would checkout a branch) to go back: $ git checkout <commit-id> .

How do you go back to a specific commit in git? ›

To undo changes associated with a specific commit, developers should use the git revert command. To undo every change that has happened since a given commit occurred, use git reset.

How do you cherry pick a commit from another branch using TortoiseGit? ›

Cherry-picking in TortoiseGit is invoked from the Revision Log Dialog. Within this dialog, select the commit(s) to cherry-pick, then right-click on one of the selected commits to pop up the context menu. Select Cherry Pick this commit... (or Cherry Pick select commits... if more than one commit is selected).

How do I resolve conflicts after git cherry-pick? ›

git cherry-pick <commit-hash> to cherry-pick a commit from another branch. git cherry-pick -n <commit-hash> to cherry-pick the commit but it won't commit the changes. It will only stage all the changes. git cherry-pick —continue or git cherry-pick —abort when you face conflicts while cherry-picking.

Does git cherry-pick preserve history? ›

According to the Git docs the action of the git cherry-check command is to "Apply the changes introduced by some existing commits". Notice the use of the phrase apply changes rather than merge changes. Cherry picking will not preserve history exactly how it is represented from the source.

Is it possible to get merge conflict during git cherry-pick? ›

Yes, at least with the standard git setup. You cannot cherry-pick while there are conflicts. Furthermore, in general conflicts get harder to resolve the more you have, so it's generally better to resolve them one by one.

How do I cherry pick a commit from another branch in Visual Studio? ›

Visual Studio 2017

Open up Team Explorer and check out the branch you want to cherry-pick changes into using the Branches view. Right-click the branch containing the changes you want and select View History.... Right-click the commit you want to cherry-pick and select Cherry-pick.

How do you cherry pick a commit from another branch in Android Studio? ›

Apply a commit to another branch

Open the Git tool window Alt+9 and switch to the Log tab. Locate the commit containing the changes you want to cherry pick. on the toolbar). Select the required commit.

How do you cherry pick a commit from another branch bitbucket? ›

Use git cherry pick. It lets you pick commit and add them over other branches. This will add the commit on the current branch. Save this answer.

How do I cherry pick a branch in git? ›

In the cherry-pick command's simplest form, you can just use the SHA identifier for the commit you want to integrate into your current HEAD branch. Once you know the commit hash, you can use the cherry-pick command. This will dedicate the specified change to your currently checked-out branch.


1. Cherry picking Git commits | Advanced Git - 10
(Bogdan Stashchuk)
2. 016 Introduction to Git cherry-pick
(Dan Gitschooldude)
3. git cherry-pick (intermediate) anthony explains #068
4. Git cherry pick in GitHub desktop
(Devops buzz with Kirtan)
5. 18 - Git Cherry Pick Commit
(Ankit Srivastava)
6. 20. GIT Cherry-pick. Handle bugfix or Hotfix by cherry picking a commit into another branch - GIT
(Leela Web Dev)
Top Articles
Latest Posts
Article information

Author: Fredrick Kertzmann

Last Updated: 01/18/2023

Views: 5536

Rating: 4.6 / 5 (46 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Fredrick Kertzmann

Birthday: 2000-04-29

Address: Apt. 203 613 Huels Gateway, Ralphtown, LA 40204

Phone: +2135150832870

Job: Regional Design Producer

Hobby: Nordic skating, Lacemaking, Mountain biking, Rowing, Gardening, Water sports, role-playing games

Introduction: My name is Fredrick Kertzmann, I am a gleaming, encouraging, inexpensive, thankful, tender, quaint, precious person who loves writing and wants to share my knowledge and understanding with you.