Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

git
cherry-pick
communitycreator

How to cherry-pick a commit in Git

Anjana Shankar

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring