Trusted answers to developer questions

How to cherry-pick a commit in Git

Get Started With Machine Learning

Learn the fundamentals of Machine Learning with this free course. Future-proof your career by adding ML skills to your toolkit — or prepare to land a job in AI or Data Science.

As a programmer, each one of us has been in a situation where we want to pick a specific commit id from a different branch to the one you are currently working on. There is a very simple helpful git command that allows you to do this.

git cherry-pick <commit_sha>

You can check the logs and find the commit sha of the commit you want to write on your branch. Note that this commit forms the head of your current branch.

Let’s try to understand the effect using the following diagram. Assume that the following nodes demonstrate your commits into a certain repository.

We have two branches that branched out at b.

g a a b b a->b c c b->c g g b->g d d c->d e e d->e f f e->f h h g->h

So, cherry-picking the e commit will lead to the following effect.

g e1 e f f e1->f a a b b a->b c c b->c g g b->g d d c->d d->e1 h h g->h e e h->e

Options

  1. --edit: This allows you to edit the commit message before writing the commit onto your working tree.

  2. -x: This appends a line recording the original commit from where this commit is being cherry-picked. This option is useless if you are trying to cherry-pick from a private branch, and is only useful when the commit is being cherry-picked between two public branches. For example, this option can be super useful when trying to cherry-pick a commit between two maintenance branches.

  3. --gpg-sign: Allows you to GPG sign the commit.

RELATED TAGS

git
cherry-pick
Did you find this helpful?