Git Cherry-Picking

What is Git Cherry-Picking?

Git is a distributed version control system for tracking changes in source code during software development. It is designed for coordinating work among programmers, but it can be used to track changes in any set of files. Its goals include speed, data integrity, and support for distributed, non-linear workflows.

Cherry picking in git means to choose a commit from one branch and apply it onto another.
This is in contrast with other ways such as merge and rebase which normally apply many commits onto another branch. In other words, cherry-picking is when you want to take the contents of a single commit from another branch and copy-paste them to a destination branch as a brand new commit.

Quick Summary of Git Cherry-picking steps

  • Find the commit hash you want to cherry-pick.
  • Go to your destination branch.
  • git cherry-pick -x commit-hash
  • Resolve conflicts if they occur.
  • If there are notes in the original commit, copy them over.

Detailed Git Cherry-picking process

  1. From the origin branch where you want to cherry pick from, run this command to get the commit has needed:

    git reflog.

  2. Make sure you are on the branch you want to apply the commit to.
    git checkout
    Execute the following for the typical cherry-pick command is simply:
    git cherry-pick commit-hash
    As developers, we often perform a series of steps to achieve our objectives. Showing you an isolated cherry-pick command is the same as throwing you the manual and asking you to read it.
  3. If you cherry-pick from a public branch, you should consider using
    git cherry-pick -x commit-hash
    This will generate a standardized commit message. This way, you (and your co-workers) can still keep track of the origin of the commit and may avoid merge conflicts in the future.
  4. If you have notes attached to the commit they do not follow the cherry-pick. To bring them over as well, You have to use:
    git notes copy from_commit_hash to_commit_hash