Cherry-Pick In Git Explained

Git

03/06/2021


The command cherry-pick allows you to copy a commit from one branch to another. Assume you have the following commit history in branch B, which you can retrieve with git log --oneline.

BASH
e061ddd Some commit message
e215bb2 Another commit message
e0401f2 Bananas

Back in branch A, you can then copy the 1st commit from branch B with git cherry-pick e061ddd. This will apply the cherry picked commit as a new commit to the current branch head. 👤

Options

On top of this, cherry-pick offers a few neat options you might fancy to use.

Multiple copies

To copy multiple commits, simply append more hashes at the end of the command.

BASH
git cherry-pick e061ddd <another-commit-hash>

Each cherry-picked commit will be added as a new separate commit to your branch.

Stage instead of commit

Perhaps you want to copy a commit without directly committing it to the current branch? Don't worry, we got you covered. 😜

BASH
git cherry-pick e061ddd -n

Rename it

Likewise, you can rename a cherry-picked commit with

BASH
git cherry-pick e061ddd -e

WRITTEN BY

Code and stuff