Git is a powerful tool for version control that allows developers to collaborate on code and keep track of changes.
One of the features that make Git so powerful is its ability to cherry-pick specific commits from a branch and apply them to another branch. This can be incredibly useful in situations where you need to apply a specific change from one branch to another, without merging the entire branch.
In this Git Cherry Pick command tutorial, we will explore how to use the cherry-pick command in Git to pick the right commit for your code. We will cover the basics of cherry picking, including how to pick a commit, resolve conflicts, and more.
Table of Contents
What is Git Cherry Pick Command?
Git cherry-pick is a command that allows you to select a single commit from one branch and apply it to another branch. This is done by creating a new commit on the target branch, which contains the changes introduced by the selected commit.
The cherry-pick command is a powerful tool that can save you a lot of time and effort when working with Git. It can be used to apply specific changes from one branch to another, without having to merge the entire branch.
git cherry-pick commit
Specially, we have seen its use when a release branch is cut and then we have to introduce some emergency changes.
How to Cherry Pick a Commit?
Cherry picking a commit is a simple process that involves using the
git cherry-pick command followed by the commit ID. Here are the steps to cherry-pick a commit:
Step 1: First, switch to the branch where you want to apply the commit.
git checkout <branch>
Step 2: Get the commit ID of the commit you want to cherry-pick. You can get the commit ID by running the command git log or directly from the GitHub web.
Step 3: Once you have the commit ID, run the following command to cherry-pick the commit:
git cherry-pick <SHA>
Step 4: Finally, push the changes to the remote branch with git push command.
Git Cherry Pick Example
So now let’s take an example where a company is cutting its release for any application with release branch v1.0.
So first of all, one of the developers creates a release branch
git checkout -b v1.0
Right now everything is good, but suddenly, one of the testers tells the developer that there is a critical bug in the current release branch, so he will create another branch called “critical-bug-fix” with the fix and ask the developer to cherry-pick it.
Just for simplicity, we are creating a new branch and adding a file that will fix the bug, and pushing that branch to the main repository for merging.
➜ cherry-pick git:(main) git checkout -b critical-bug-fix Switched to a new branch 'critical-bug-fix' ➜ cherry-pick git:(critical-bug-fix) echo "This will fix the bug" > fix.txt ➜ cherry-pick git:(critical-bug-fix) ✗ git add fix.txt ➜ cherry-pick git:(critical-bug-fix) ✗ git status On branch critical-bug-fix Changes to be committed: (use "git restore --staged <file>..." to unstage) new file: fix.txt ➜ cherry-pick git:(critical-bug-fix) ✗ ➜ cherry-pick git:(critical-bug-fix) ✗ ➜ cherry-pick git:(critical-bug-fix) ✗ git commit -m "Bug fix - please cherry-pick it" 1 file changed, 1 insertion(+) create mode 100644 fix.txt ➜ cherry-pick git:(critical-bug-fix) git push origin critical-bug-fix Enumerating objects: 12, done. Counting objects: 100% (12/12), done. Delta compression using up to 8 threads Compressing objects: 100% (6/6), done. Writing objects: 100% (12/12), 2.11 KiB | 2.11 MiB/s, done. Total 12 (delta 2), reused 8 (delta 1), pack-reused 0 remote: Resolving deltas: 100% (2/2), done. remote: remote: Create a pull request for 'critical-bug-fix' on GitHub by visiting: remote: https://github.com/storagetutorials/cherry-pick/pull/new/critical-bug-fix remote: To github.com:storagetutorials/cherry-pick.git * [new branch] critical-bug-fix -> critical-bug-fix ➜ cherry-pick git:(critical-bug-fix)
So now, assume that the pull request is opened and bug fix branch is merged and the developer has to cherry-pick that particular commit with the fix using the
git cherry-pick command.
First, the developer copies the commit ID or SHA that needs to be cherry-picked like the below screenshot
Then checkout to the branch where this commit needs to be applied and run the git command.
➜ cherry-pick git:(main) git checkout v1.0 Branch 'v1.0' set up to track remote branch 'v1.0' from 'origin'. Switched to a new branch 'v1.0' ➜ cherry-pick git:(v1.0) ➜ cherry-pick git:(v1.0) git cherry-pick 8f54caa2404bf392b366587aade07be3b981372b [v1.0 ed23a66] Bug fix - please cherry-pick it Date: Sat Feb 25 00:02:03 2023 -0500 1 file changed, 1 insertion(+) create mode 100644 fix.txt
Once the desired commit is cherry-picked, the changes can be pushed to the remote branch using the
git push command.
➜ cherry-pick git:(v1.0) git push Enumerating objects: 4, done. Counting objects: 100% (4/4), done. Delta compression using up to 8 threads Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 314 bytes | 314.00 KiB/s, done. Total 3 (delta 1), reused 0 (delta 0), pack-reused 0 remote: Resolving deltas: 100% (1/1), completed with 1 local object. To github.com:storagetutorials/cherry-pick.git cf582db..ed23a66 v1.0 -> v1.0
That’s it! Now, this release branch is ready to be used with the new fix.
How to Resolve Conflicts while Cherry Picking?
Sometimes, when you cherry-pick a commit, conflicts can arise if the changes introduced by the selected commit conflict with changes on the target branch.
In such cases, Git will mark the files with conflicts and prompt you to resolve them manually.
To resolve conflicts while cherry-picking a commit, you need to follow these steps:
Step 1: Run the git cherry-pick command as usual to apply the commit.
Step 2: When conflicts arise, open the files marked with conflicts and resolve them manually.
Step 3: Once you have resolved all conflicts, add the changes to the staging area using the git add command.
Step 4: Finally, commit the changes using the git commit command.
Cherry Picking Multiple Commits?
In some cases, you may need to cherry-pick multiple commits instead of just one. This can be done using the git cherry-pick command with multiple commit IDs.
Here are the steps to cherry-pick multiple commits:
Step 1: Get the commit IDs of the commits you want to cherry-pick using the git log command.
Step 2: Run the following command to cherry-pick the commits:
git cherry-pick Commit-id-1 commit-id-2
We hope you are able to understand what is git cherry pick, how to use it, and how to resolve conflicts. If you have any questions or concerns, please let us know through your comments.