a concise shot of dev knowledge
Become a Contributor
Concise shots of dev knowledge

RELATED TAGS

git
cherry-pick
communitycreator

How to cherry-pick a commit in Git

Anjana Shankar

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
communitycreator
RELATED COURSES

View all Courses

Keep Exploring

Learn in-demand tech skills in half the time

Copyright ©2022 Educative, Inc. All rights reserved.

soc2