What Does Git Cherry Pick Do, And When Should You Use It? (2024)

Quick Links

  • What Is Git Cherry Pick, And How Does It Work?

  • Why Cherry Pick?

  • Using Git Cherry Pick

git cherry-pickis a simple but powerful tool that allows you to selectively transfer commits from one branch to another. You can use it when you don't want to merge an entire branch into master, but would still like to include changes from a feature branch.

What Is Git Cherry Pick, And How Does It Work?

In Git, commits are the units that track changes over time. Each commit might represent a bug fix, new feature, or part of a larger solution. You can think of commits being stored as a linked list pointing back in time; whenever you change your branch HEAD, Git reconstructs your local directory files by taking into account all commits going back to the beginning.

This is a simplification of course, and Git doesn't technically store commits as simple lists of changes, it uses file-based blobs, but the principle is the same.

Things get more complicated when branches get involved. Often, work on a feature takes many days or weeks. Rather than clutter up the main repository, it goes into a separate feature branch, and is then code reviewed and merged back in. Merging is basically branching this commit history so that the entire feature branch gets included when Git does its thing.

What Does Git Cherry Pick Do, And When ShouldYou Use It? (1)

However, what if you want to merge some things, but don't want the entire branch to get included just yet? You can't do

git merge 

in that case.

This is where git cherry-pickbecomes useful. It does what the name implies---takes a single commit from the feature branch, picks it out individually, and applies it to the master branch, or vice versa. This copies the commit, so that there's a brand new commit on the target branch.

What Does Git Cherry Pick Do, And When ShouldYou Use It? (2)

Note that there's no real "line" connecting the new commit on the master branch to the old commit. The new cherry-picked commit created on master does not reference the source commit at all, the commit is simply copied. Git will take care of it when you merge the branches back together, because the commits are seen as copies of each other.

In effect, the commit is "transferred" over to the new branch, though you will have to keep in mind that the old commit is still applied to the feature branch. You wouldn't want to revert the source commit though, since that will cause issues when merging later on.

Why Cherry Pick?

There are a few uses cases for this. Perhaps you're working on a feature, and it's not ready for release, but you fixed a bug on the feature branch that you'd like to get into your weekly release version. You can use cherry-pick to copy the bug fix to Master to deploy it early.

Or perhaps you have multiple branches for production and development, and you'd like to copy an urgent bug fix from production to development. Cherry pick can do that too.

Or you may have even just accidentally made a commit to the wrong branch. You can use git cherry-pickto copy it to the right one, and then

git reset 

to undo that commit (provided it's at the HEAD of the branch).

Using Git Cherry Pick

Once you understand what you're doing, git cherry-pickis pretty simple to use.

The easiest way to view Git's commit history on the command line is with the following command, which shows all commits but with a branch and merge history, which is crucial for using cherry-pick:

git log --pretty=format:"%h %s" --graph
What Does Git Cherry Pick Do, And When ShouldYou Use It? (3)

Then, you can simply copy the SHA1 hash of the commit, and run the cherry pick:

git cherry-pick 1da76d3

Note that you can also cherry pick multiple commits at the same time, just pass in multiple hashes.

git cherry-pickhas a few useful options:

  • --no-commit only applies the changes to your directory. You will need to stage and commit manually.
  • --editwill let you change the commit message.
  • -xwill add a message saying "cherry picked from commit: ..."

Of course, if you're using a GUI based Git client like Fork or GitKraken, which we highly recommend, you can just right click on the commit and choose to cherry pick it:

What Does Git Cherry Pick Do, And When ShouldYou Use It? (4)
  • Linux
  • Programming

Your changes have been saved

Email is sent

Email has already been sent

Please verify your email address.

You’ve reached your account maximum for followed topics.

Manage Your List

Follow

Followed

Follow with Notifications

Follow

Unfollow

Readers like you help support How-To Geek. When you make a purchase using links on our site, we may earn an affiliate commission. Read More.

What Does Git Cherry Pick Do, And When Should You Use It? (2024)

References

Top Articles
Leak Under the Kitchen Sink? Here’s How to Fix it
How to Fix a Bathroom Sink Leak | House+Garden
Bubble Guppies Who's Gonna Play The Big Bad Wolf Dailymotion
Www.1Tamilmv.cafe
Overton Funeral Home Waterloo Iowa
Skamania Lodge Groupon
Nyu Paralegal Program
Mcfarland Usa 123Movies
Ingles Weekly Ad Lilburn Ga
Practical Magic 123Movies
When is streaming illegal? What you need to know about pirated content
Comcast Xfinity Outage in Kipton, Ohio
Call of Duty: NEXT Event Intel, How to Watch, and Tune In Rewards
Lichtsignale | Spur H0 | Sortiment | Viessmann Modelltechnik GmbH
Fcs Teamehub
[2024] How to watch Sound of Freedom on Hulu
Shariraye Update
Wordle auf Deutsch - Wordle mit Deutschen Wörtern Spielen
Persona 4 Golden Taotie Fusion Calculator
Best Nail Salon Rome Ga
Youravon Comcom
Average Salary in Philippines in 2024 - Timeular
Kcwi Tv Schedule
Touchless Car Wash Schaumburg
Www.publicsurplus.com Motor Pool
Xfinity Cup Race Today
eugene bicycles - craigslist
Marilyn Seipt Obituary
Mikayla Campinos: Unveiling The Truth Behind The Leaked Content
Xpanas Indo
Generator Supercenter Heartland
Greyson Alexander Thorn
Walter King Tut Johnson Sentenced
Workday Latech Edu
Oreillys Federal And Evans
Bismarck Mandan Mugshots
About :: Town Of Saugerties
Top 25 E-Commerce Companies Using FedEx
Electric Toothbrush Feature Crossword
Tunica Inmate Roster Release
Eat Like A King Who's On A Budget Copypasta
Dancing Bear - House Party! ID ? Brunette in hardcore action
Rovert Wrestling
Google Flights Missoula
Is TinyZone TV Safe?
Wera13X
Diccionario De Los Sueños Misabueso
Otter Bustr
Glowforge Forum
Worlds Hardest Game Tyrone
7 National Titles Forum
Latest Posts
Article information

Author: Carlyn Walter

Last Updated:

Views: 6402

Rating: 5 / 5 (50 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Carlyn Walter

Birthday: 1996-01-03

Address: Suite 452 40815 Denyse Extensions, Sengermouth, OR 42374

Phone: +8501809515404

Job: Manufacturing Technician

Hobby: Table tennis, Archery, Vacation, Metal detecting, Yo-yoing, Crocheting, Creative writing

Introduction: My name is Carlyn Walter, I am a lively, glamorous, healthy, clean, powerful, calm, combative person who loves writing and wants to share my knowledge and understanding with you.