The Wayback Machine - https://web.archive.org/web/20240531031457/https://www.geeksforgeeks.org/git-cherry-pick/
Open In App

Git – Cherry Pick

Last Updated : 01 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

git cherry-pick in git means choosing a commit from one branch and applying it to another branch. This is in contrast with other ways such as merge and rebases which normally apply many commits into another branch.

git cherry-pick is just like rebasing, an advanced concept and also a powerful command. It is mainly used if you don’t want to merge the whole branch and you want some of the commits.

When to use cherry-pick?

Suppose a developer fails to recognize which branch he is currently on, and by mistake, he commits to another branch instead of committing to the main branch. Now to fix it, he has to first run git show, then save the commit, check out the main branch, apply a patch there, and commit with the same commit message. But all this can be done automatically by using just one command i.e. cherry-pick.

In order to understand better refer to the below diagram as follows:

Before Cherry Pick

Before Cherry Pick

After Cherry Pick

After Cherry Pick

The command for Cherry-pick is as follows: 

git cherry-pick<commit-hash>

Commit hash: A commit hash is a unique identifier that is generated by Git. Each commit has its one commit hash.

Note: While using this command make sure you are on the branch you want to apply the commit.

How to use cherry-pick?

Here is the step-by-step explanation of the use of cherry-pick command in the below-created project stepwise shown below as follows: 

Step 1:  Opening the git bash and creating a new project named sample and initializing the repo using the git init command.

 

Step 2: Creating a ‘.txt’ file using vi command to the project let’s say an index file and add it to our sample project and make a commit and write a commit message before pressing the Enter.

Note: After running the vi <file_name> command , type :wq to save and quit the file. 

 

One can check your commit by git log command easily:

 

Step 3: Now assume we have 2 versions, so create 2 different branches by using the git branch command and move to a branch, let us say 2 by using git checkout command.

Note: One can simple view all the branches by running the git branch command as shown in the below diagram.

 

Step 4: Now suppose you want to work on some new feature, so creating and adding a new feature file lets say feature.txt using vi and add command respectively as shown below. Then commit your changes with a commit message.

 

One can check your commit by git log command as shown below:

It is clearly showing our first commit where our branch 1 is there and in branch 2 it has moved away farther ahead and we are currently working on our feature in branch 2

Step 5:  Now suppose we found a bug in our feature and we came to know that this same bug is also present in our 1 branch. 

And now we are trying to fix some bug or issue as shown below by adding a fix.txt file let’s suppose and add it to the current branch i.e. 2 and committing the required changes.

 

Checking our final commits:

 

Step 6: Now, we have fixed the bug in branch 2, but we also need to add this fix to our branch 1 also, but we don’t want to merge this branch 2 into our branch 1, because the work might still be going on the feature. 

Thus, in this scenario, we can cherry-pick this particular commit. To do so, just copy the hash value highlighted in the above diagram, then move to branch 1 by using checkout, and then use the command cherry-pick and paste the hash we just copied.

 

As seen clearly from the above, then we notice that previously we have only index.txt before doing cherry-picking, but now we have the fix.txt file also in our 1st branch.

Now if we try to check git log –oneline, we will be able to see that commit also came in the branch 1.

 

Some important Use Cases of Cherry-pick

The following are some common applications of Cherry-Pick:

  1. If you by mistake make a commit in an incorrect branch, then using cherry-pick you can apply the required changes.
     
  2. Suppose when the same data structure is to be used by both the frontend and backend of a project. Then a developer can use cherry-pick to pick the commit and use it to his/her part of the project.
     
  3. At the point when a bug is found it is critical to convey a fix to end clients as fast as could be expected.
     
  4. Some of the time a component branch might go old and not get converged into the main branch and the request might get closed, but since git never loses those commits, it can be cherry-picked and it would be back.

Disadvantages of using Cherry Pick

Cherry-pick should not be used always as it can cause copy commits and numerous situations where cherry-picking would work, conventional merges are liked all things considered. Also, in the situation where the commits from 2 or more branches update similar lines of code with various substances and git cherry-pick one commit to the other branch, it prompts conflict as well.

Conclusion

Git’s cherry-pick command is a powerful tool for selectively applying specific commits between branches. It’s great for fixing mistakes and sharing code, but overusing it can lead to issues, especially when commits touch the same code lines. So, use it wisely to maintain a clean and efficient code history.


Previous Article
Next Article

Similar Reads

How to Use the "cherry-pick" Command in Git?
Git, with its myriad of commands and functionalities, offers developers powerful tools to manage project history efficiently. Among these commands, "cherry-pick" stands out as a versatile feature for selectively applying commits from one branch to another. In this guide, we'll delve into the intricacies of the "cherry-pick" command, exploring its a
3 min read
Difference Between Git remote prune, Git prune and Git fetch --prune
One of the great things about Git is that it's *very careful about deleting data*. This makes it pretty hard to lose commits or other valuable data in Git! A small downside of this is that you might sometimes see stale data that you actually don't need anymore. One of the best examples of this is references to remote branches that have already been
2 min read
Difference between “git add -A” and “git add”
Git is a free and open-source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. It is basically, software for tracking changes in any set of files, usually used for coordinating work among programmers collaboratively developing source code during software development. Featu
3 min read
Git - Difference Between Git Revert, Checkout and Reset
While Working with Git in certain situations we want to undo changes in the working area or index area, sometimes remove commits locally or remotely and we need to reverse those changes. There are 3 different ways in which we can undo the changes in our repository, these are git reset, git checkout, and git revert. git checkout and git reset in fac
7 min read
Git - git-show Command Line Utility
Git is a free and open-source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. Git relies on the basis of distributed development of software where more than one developer may have access to the source code of a specific application and can modify changes to it that may be
3 min read
Git LFS: Managing Large Files in Git Repositories
Git, undoubtedly one of the most popular version control systems, is widely used for managing source code in software development projects. However, one of its limitations is its handling of large files. Traditional Git repositories struggle to efficiently manage large files, leading to bloated repositories and slow performance. This is where Git L
4 min read
Git Subtree vs. Git Submodule
Git Subtree and Git Submodule are both mechanisms in Git that allow you to incorporate external repositories into your own repository. They provide a way to manage dependencies and include code from other projects while keeping your repository organized and maintainable. Git Subtree: Git Subtree allows you to insert a separate repository as a subdi
4 min read
Difference Between Git Fetch and Git Pull
Understanding the difference between git fetch and git pull is important for effective version control in Git. These commands are important for managing your repository and collaborating with team members. In this article, Let us look at Git Fetch and Git Pull separately with the help of an example. What is Git Fetch?The Git Fetch command is used t
4 min read
Difference Between Git Push Origin and Git Push Origin Master
Understanding the difference between git push origin and git push origin master is important for efficient version control in Git. These commands are used to upload changes from your local repository to a remote repository, but they function differently. This article will explain these differences, how each command works, and when to use them. What
3 min read
How to Set Up Git Using Git Config?
Git is a powerful version control system that helps developers manage and track changes in their code. Setting up Git correctly is important for maintaining an organized and efficient workflow. In this article, we'll walk you through the process of setting up Git using the git config command. PrerequisitesBefore we dive into the setup process, make
3 min read