git cherry-pick is a powerful command that enables arbitrary git commits to be picked by reference and appended to the current working HEAD. The command is typically used to selectively choose particular commit(s) from one branch within a repository onto a different branch. It applies the changes introduced by the named commit (identified by SHA1 reference) on the current branch and will introduce new, distinct commit. Strictly speaking, using
git cherry-pick doesn’t alter the existing history within a repository; instead, it adds to the history. This command offers more fine-tuned selection than a general
When to use cherry-pick
If you are working with a team of developers, managing the changes between a numerous git branches can become a complex task. Sometimes you don’t want to merge a whole branch into another, but instead you only need to pick specific commits. Or
Maybe the backend team creates a data structure that the frontend team will also need to utilize. The frontend team could pick only the commit in which this hypothetical data structure was created. This pick would enable the frontend team to continue progress on their side of the project. Or
A critical bug is discovered and it is important to deliver a fix to end users as quickly as possible. One of the team members already fixed this bug while working on a new feature. The developer has created an explicit commit patching this bug. This new patch commit can be picked directly to hot-fix branch to fix the issue before it affects more users.
Here is the graph of the commits from
feature branch. We will focus on the commit
feature branch which needs to be applied on
Make sure you are on the branch you want to apply the commit to.
$ git checkout master
Now we can apply the commit
$ git cherry-pick dbb6ffd
You can now verify that
dbb6ffd has been applied to the
master branch via the log listing. Note that
dbb6ffd in the
master has different SHA1 value, which is
As any other git command,
git cherry-pick has numerous options, we will cover some of them:
- standardize commit message: When recording the commit, append a line that says “(cherry picked from commit …)” to the original commit message in order to indicate which commit this change was cherry-picked from. It is better to use standardize commit message if we cherry-pick from a public branch.
$ git cherry-pick -x <commit-hash>
This will generate a standardized commit message. This way, your team can still keep track of the origin of the commit and may avoid merge conflicts in the future.
- copy over the notes: Sometimes some of cherry-picked commits have notes which are not copied. Therefore, to bring them over as well, we will need to use:
$ git notes copy <from-commit-hash> <to-commit-hash>
- cherry-pick multiple commits: We want to cherry-pick multiple commits, if they are linear, then only use the below command,
$ git cherry-pick <from-commit-hash>..<to-commit-hash>
<from-commit-hash> you will need to use caret symbol (
- editing commit message: In some branches, the commit may be useful for different reasons. As a consequence, you may want to change the commit message when cherry-picking.
$ git cherry-pick -e <commit-hash>
- moving without committing: The
--no-commitoption will execute the cherry pick but instead of making a new commit it will move the contents of the target commit into the working directory of the current branch.
$ git cherry-pick --no-commit <commit-hash>
- adding signature: The
--signoffoption will add a 'signoff' signature line to the end of the cherry-pick commit message
$ git cherry-pick --signoff <commit-hash>
In addition to these helpful options
git cherry-pick also accepts a variety of merge strategy options.
git cherry-pick also accepts option input for merge conflict resolution, which are: