Typefully

What is "git cherry-pick"?

Avatar

Share

 • 

4 years ago

 • 

View on X

Git: What is Cherrypick? a thread...
The dictionary meaning of cherry-picking is to pick the best item from the group of items. So what does that mean in the context of git?
When we want to pick a single commit from the source branch and want to apply it to the destination branch, in such a case we use the "git cherry-pick" command. Here we're cherry-picking the commit from all the commits present in the source branch and copying it to dest. branch.
How to do it? By using the command: "git cherry-pick <commit-hash>" Let's understand this with an example
Consider, that we've two branches "master" and "feature" branch with the following commits on them:
Let's say we want to add f2 commit from the feature to the master branch: 1. We've to checkout to the master branch using: "git checkout master" 2. Use git cherry-pick as following: "git cherry-pick f2" You've successfully added f2 commit in master now.
This is what it will look like in the end:
When to use "git cherry-pick"? 1. Helpful in restoring lost commit 2. Undoing any wrong commit made to a wrong branch and adding to the correct branch
3. Hot fixing a specific bug and moving such a bug to the master branch. 4. Sharing specific changes to other Dev working on similar requirements.
Conclusion: Only use the "cherry-pick" command when you genuinely require it as mentioned above. git cherry-pick is a very powerful command but its use should be done carefully otherwise it might add duplicate commits. For all other work, git merge is preferable.
Thanks for Reading, If you like this then you might also like reading my previous thread on git stash: twitter.com/vikasrajputin/status/1520951975021453312
Namaste, I'm Vikas! I write a thread every Mon, Wed & Fri on Java, Javascript & Fullstack Development. To read all my future threads follow @vikasrajputin Any Query, Feedback & Suggestions? Put them in the comments below. Happy Coding!
Avatar

Vikas Rajput

@vikasrajputin

Sr. Backend Engineer. Simplifying Backend and sharing what I learn along the way. Building Gujarat's biggest java community at @juggujarat