Skip to content
Go back

Git cherry-pick Command

Published:  at  09:00 AM

git cherry-pick command allows you to select one or more specific commits from anywhere in the repository history and apply them as new commits on the branch you are current on.

1. Usage

git cherry-pick <commitHash>...

1.1 Pick single commit

If your Git repository have two branches(main and feature):

a - b - c - d - e  HEAD -> main
     \
       f - g - h  feature

To apply commit f from feature branch to current branch, use:

git cherry-pick f

Then the commit f will be replayed and added as a new commit in your current branch.

a - b - c - d - e - i  HEAD -> main
     \
       f - g - h  feature

Commit i is a new commit. The commit hash is different from f, but the changes are the same.

1.2 Pick multiple commits

To apply multiple commits to git repository, use a list of commit:

git cherry-pick f g h

Git repository will be like:

a - b - c - d - e - i - j - k  HEAD -> main
     \
       f - g - h  feature

If you need to cherry-pick a long list of commits, there is no need to write down every commit hash. Just use:

Of course, f..h need to be continus commits.

1.3 Use branch name

Instead of using <commitHash> to pick specific commit, you can directly use <branch-name> as a target, use a branch name will pick specific branch’s latest commit.

For the previous example, just use git cherry-pick feature and it will apply the commit h to the current branch.

1.4 Resolve Conflict

If your git cherry-pick command encounters a conflict, you will need to resolve the conflict first.

Then add the resolved file to the staging area and use the command git cherry-pick --continue to proceed.



Previous Post
Endianness
Next Post
Understanding Immutable Strings in Java