git cherry-pick command: multiple commits, conflict, cancel, options
This article will cover the git cherry-pick
command. We'll cover the basics, how to use it, how to handle multiple commits, conflict management, order, and the different options.
1. What is git cherry-pick?
Cherry picking in Git is the process of copying one or more desired commits from one branch and inserting them into another branch.
It's a very powerful feature, depending on how you use it, but it can also clutter up your history. You can do this with the git cherry-pick
command.
In particular, the git cherry-pick
command is used for things like urgent bug fixes, or synchronizing partial code between branches.
Otherwise it is good practice to use relatively safe methods like git merge and git rebase, even in collaborative situations.
is a good process.
So let's start by understanding the usage of the git cherry-pick
command so that we can use it immediately when we need to.
2. The basics of using cherry-pick
Using the git cherry-pick
command is quite simple. First navigate to the branch where you want to add the commits, and then enter the commit IDs you want to pull from the other branch, like this
$ git cherry-pick [commit-id]
Commits with the same changes as that commit will then register as new commits in the current branch.
For example, suppose you currently have the following commit history
A - B - C - D main
\ E - F - G feature-a
Suppose a bug is found on the main
branch, and a developer working on the feature-a
branch makes a commit G
to the feature-a
branch that fixes the bug.
At this point, you need to get commit G into the main
branch to quickly restore the service.
Use the git cherry-pick
command to add this commit, as shown below.
$ git cherry-pick [commit-id-G]
After that, your commit history will look like this
A - B - C - D - H main
\ E - F - G feature-a
This means that all the changes from commit G
have been added to the new commit named H
.
3. Cherry-picking multiple commits
There are two ways to import multiple commits at once.
3.1. Cherry picking
The first method is to enter the commits one at a time. Enter the desired commit IDs one by one as follows.
$ git cherry-pick [commit-id-1] [commit-id-2] [commit-id-3]
This is the same pattern as when you add multiple files and folders using the git add command . In this case, they will be added as new commits from the front.
3,2. Import by scope
The second method is to specify a range of commits to import. You can use either ..
or ...
, and cherry-picking treats both equally.
You can use something like this, in which case [older-commit-id]
will be excluded and copied starting with the later commits.
$ git cherry-pick [older-commit-id]..[new-commit-id]
If you want to pull commits F
and G
from the feature-a
branch we saw in Section 2, you would type the following
$ git cherry-pick [commit-id-E]..[commit-id-G]
If you want to include [older-commit-id]
commits as well, you can also use the ^...
symbol, which means the following command will produce the same result as the above command.
$ git cherry-pick [commit-id-F]^..[commit-id-G]
4. Resolve or cancel cherry-pick conflicts
When you perform a git cherry-pick
you are committing new changes, which of course introduces the possibility of conflicts.
In this case, you should manually resolve conflicts before continuing with the cherry-pick, just as you would with a merge or rebase.
This process follows the same pattern as a rebase. After you have manually resolved conflicts, you can use the following two options with the command to complete or cancel cherry-picking.
To finish, issue the command
$ git cherry-pick --continue
To abort, use the following command
$ git cherry-pick --abort
5. Commonly used options
The following commands are often used in conjunction with the git cherry-pick
command. Familiarize yourself with them and use them in conjunction with the command as needed.
5.1. -n, --no-commit options to wait without committing
By default, the git cherry-pick
command expects to create new commits.
Sometimes, however, you want to get a specific commit, but commit it after adding another change.
To do this, you'll need the commit generated by cherry-pick
and another commit.
The -n'
or --no-commit
options are useful in this case because they don't commit the changes brought in by the cherry-pick
command, but leave them in a staging state.
This allows you to add the changes you want without committing them separately, and then commit them all at once.
5.2. -e and --edit options to change the commit message
Normally the git cherry-pick
command automatically generates a commit message that looks like this
cherry-pick [commit-id] [commit-message-of-picked-commit]
If you want to open an editor to modify this commit message or add more information, you can use the -e
or --edit
options.
5.3. --signoff option to show author of imported commits
If you're working on an open source project, or collaborating on an open source project, and you're using the cherry-pick feature, you'll often have a convention for showing the author
of imported commits.
In this case, the --signoff
option can be used to automatically add the author
of the previous commit to the commit message, without having to write it manually.
For example, if your previous commit message looked like this
Implement new feature ABC
The message for new commits cherry-picked with the --signoff
option is automatically written as shown below.
Implement new feature ABC
Signed-off-by: Your Name [your.email@example.com]
5.4. Using the --strategy option to specify a merge strategy
Due to the potential for conflicts during merges, the cherry-pick
command also allows you to manually specify a merge strategy.
The most common merge strategies, including the current default ort
strategy, are summarized in the last section of the git merge post.
The commands are
$ git cherry-pick --strategy [merge-strategy]
6. Conclusion
The git cherry-pick
command is a powerful command to have at your fingertips when you're working on a project and need to collaborate closely.
I hope you've learned it well enough to use it when the time comes.
