The git cherry-pick
command lets you pick a specific commit from one branch and apply it to another branch. It’s handy when you need to move individual changes without merging entire branches.
Initial Example
$ git cherry-pick <commit-hash>
CHERRY-PICK Options
Option | Description |
---|---|
<commit>…​ | Specifies the commits to cherry-pick. No traversal is done by default, behaving as if the --no-walk option was specified. Note that specifying a range will feed all <commit>…​ arguments to a single revision walk. |
-e , --edit | Allows you to edit the commit message before committing. |
--cleanup=<mode> | Determines how the commit message will be cleaned up before being passed to the commit machinery. For example, using scissors appends scissors to MERGE_MSG in case of a conflict. |
-x | Adds a line to the commit message indicating which commit this change was cherry-picked from, useful for publicly visible branches. |
-r | Previously disabled the default -x behavior, now it is a no-op as -x is not the default. |
-m <parent-number> , --mainline <parent-number> | Specifies the parent number of the mainline to replay the change relative to the specified parent, useful for cherry-picking a merge. |
-n , --no-commit | Applies changes to the working tree and index without making any commits. Useful for applying multiple commits in sequence. |
-s , --signoff | Adds a Signed-off-by trailer at the end of the commit message. |
-S[<keyid>] , --gpg-sign[=<keyid>] , --no-gpg-sign | GPG-signs commits. If <keyid> is specified, it defaults to the committer identity. --no-gpg-sign counteracts any prior --gpg-sign or the commit.gpgSign configuration. |
--ff | Performs a fast forward if the current HEAD is the parent of the cherry-picked commit. |
--allow-empty | Allows empty commits to be preserved automatically in a cherry-pick, useful for initially empty commits. |
--allow-empty-message | Allows cherry-picking commits with empty messages. |
--empty=(drop|keep|stop) | Specifies how to handle redundant commits: drop removes the commit, keep preserves it, and stop stops the cherry-pick for review. The default is stop . |
--keep-redundant-commits | Deprecated synonym for --empty=keep . |
--strategy=<strategy> | Uses the given merge strategy, specified only once. |
-X<option> , --strategy-option=<option> | Passes the merge strategy-specific option to the merge strategy. |
--rerere-autoupdate , --no-rerere-autoupdate | Controls whether the rerere mechanism updates the index after resolving conflicts. --no-rerere-autoupdate is useful for reviewing resolutions before committing. |
Examples
1. Simple Example
Suppose you have a feature-branch
and you want to apply a specific commit from this branch to main-branch
. Here’s how:
- Switch to
main-branch
:$ git checkout main-branch
- Cherry-pick the commit:
$ git cherry-pick <commit-hash>
Just replace <commit-hash>
with the actual hash of the commit you want to apply. This will copy the commit from feature-branch
to main-branch
.
2. Cherry-Pick Multiple Commits
If you need to apply multiple commits, you can specify each commit hash like this:
$ git cherry-pick <commit-hash1> <commit-hash2> <commit-hash3>
This will copy the specified commits to the current branch.
3. Cherry-Pick a Range of Commits
You can also copy a range of commits using this syntax:
$ git cherry-pick <start-commit-hash>..<end-commit-hash>
Just replace <start-commit-hash>
with the hash of the first commit in the range and <end-commit-hash>
with the hash of the last commit. This will apply all commits in the specified range to the current branch.