git cherry-pick
is a command that brings a specific commit to the current branch. As the name suggests, it's easy to understand as the act of picking a specific commit (cherry) from multiple commits.
Usage
Every commit has a unique ID called a hash value, which can be used to bring in a specific commit.
The basic command is as follows.
git cherry-pick <commit-hash>
Let's bring that commit history to main without merging.
The current main branch log is as follows.
The committed changes are brought to the main branch, and HEAD now points to the commit brought in by cherry-pick
.
You can also bring in multiple commits, or specify a range.
# Bringing in multiple commits
git cherry-pick <commit-hash1> <commit-hash2>
# Bringing in by range
git cherry-pick <start-commit>..<end-commit>
Key Options
-edit
or-e
: Allows you to edit the commit message.-no-commit
or-n
: Adds changes only to the staging area without committing.-signoff
or-s
: Adds a signature to the commit message.-x
: Adds the original commit hash to the commit message.
When to Use
- Applying Bug Fixes
- When you need to quickly apply a bug found in a specific branch to other branches
- Example: Applying an urgent bug fix from the production branch to the development branch
- Transferring Specific Features
- When you need to bring only part of a feature developed in one branch to another branch
- Example: Integrating successful features from an experimental branch into the main development line
- Release Management
- When selectively bringing in changes that should be included in a specific release
- Example: Selectively applying features to be included in the next version release
Conflict Warning
Since cherry pick is still bringing in and merging commit history, conflicts can occur at any time.
If a conflict occurs, resolve the conflict and continue with the command below.
git cherry-pick --continue
Additional options:
- Aborting Cherry-pick: If conflict resolution is complex or you want to cancel the cherry-pick
git cherry-pick --abort
This command reverts to the state before starting cherry-pick.
- Proceeding to the next commit while leaving the conflict as is: If you're cherry-picking multiple commits, you can leave the current conflict as is and proceed to the next commit.
git cherry-pick --skip
- Using 3-way merge: Using the
-m
option when cherry-picking performs a 3-way merge. This can sometimes make conflict resolution easier.
git cherry-pick -m 1 <commit>