git cherry-pick is 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.
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-pick becomes 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.
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-pick to 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-pick is 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
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-pick has a few useful options:
--no-commitonly 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:
- › Amazon’s “Sidewalk” Mesh Network Is Coming to More Devices
- › How to Transfer Messages from Android to iPhone
- › Lenovo Just Revealed New Yoga and Slim Windows Laptops
- › Apple Pay Later Is Here, but You Should Probably Stay Away
- › Steam Is Saying Goodbye to Windows 7 and 8
- › You Can Now Try Microsoft Edge’s Real-Time Tab Sharing