What does Git Cherry Pick do and when should I use it?

Contents

Git logo

git cherry-pick is a simple but powerful tool that enables you to transfer selective commits from one branch to another. You can use it when you don't want to merge an entire branch into the master, but still you would like to include changes from a function branch.

What is Git Cherry Pick and how does it work?

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

This is a simplification, in any case, and Git doesn't technically store commits as simple changelists, use file-based blobs, but the principle is the same.

Things get complicated when branches get involved. Often, working on a role takes several days or weeks. Instead of messing up the main repository, move to a separate feature branch, and then the code is reviewed and merged again. Merging is simply branching this commit history so that the entire feature branch is included when Git does its thing.

Despite this, What if you want to merge some things, but you don't want the whole branch to be included yet? You can not do git merge then.

This is where git cherry-pick becomes useful. Does what the name suggests: takes a single commit from the feature branch, selects it individually and applies it to the master branch, or viceversa. This copies the confirmation, so there is a new commit in the destination branch.

Note that there isn't a “line” Note that there isn't a. The carefully selected new confirmation created in the master does not reference the origin confirmation at all, the confirmation is simply copied. Git will take care of it when you merge the branches again, because commits look like copies of each other.

Indeed, Note that there isn't a “Note that there isn't a” Note that there isn't a, even though you should pay attention that the above confirmation still applies to the characteristics branch. Despite this, you don't want to reverse the confirmation of origin, since that will cause problems when merging later.

Why Cherry Pick?

There are some use cases for this. Maybe you're working on a feature and it's not ready to launch, but you have fixed a bug in the function branch that you would like to include in your weekly release version. You can use selective selection to copy bug fix to Master to implement earlier.

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

Or it is possible that you have even accidentally committed to the wrong branch. You can use git cherry-pick to copy it to the right, and then git reset to undo that confirmation (as long as it is at the HEAD of the branch).

Using Git Cherry Pick

Once you understand what you are doing, git cherry-pick it is quite simple to use.

The easiest way to view the Git commit history on the command line is with the following command, which shows all commits but with a history of forks and merges, which is crucial to use cherry-pick:

git log --pretty=format:"%h %s" --graph

After, you can just copy the SHA1 hash from the commit and run the cherry pick:

git cherry-pick 1da76d3

Note that you can also choose multiple confirmations at the same time, just pass multiple hashes.

git cherry-pick has some useful options:

  • --no-commit just apply the changes to your directory. You will need to prepare and confirm manually.
  • --edit will allow you to change the confirmation message.
  • -x Note that there isn't a “Note that there isn't a: …”

Of course, if you are using a GUI based Git client like Fork O GitCrack, which we strongly suggest, you can right click on the commitment and select select it:

Subscribe to our Newsletter

We will not send you SPAM mail. We hate it as much as you.