git cherry pick is a powerful command in Git that allows you to apply changes from specific commits in one branch to another branch. This is particularly useful when you want to incorporate individual changes without merging entire branches.
Basic Usage:
To apply a commit from another branch to your current branch, first ensure you’re on the target.
git checkout target-branch
Then, execute the cherry-pick command with the commit hash
git cherry-pick <commit-hash>
Common Options in git cherry pick
1. -e or -edit
- Allows you to edit the commit message before creating the new commit.
- Useful when you want to modify or add additional details to the commit message.
Example
git cherry-pick -e <commit-hash>
2. -x
- Appends a line to the commit message indicating the original commit hash.
- This is helpful for tracking where the commit originated
Example
git cherry-pick -x <commit-hash>
3. -n or –no-commit
Applies the changes from the commit(s) to the working directory without creating a new commit.
Useful if you want to review, modify, or combine changes before committing.
Example
git cherry-pick -n <commit-hash>
4. -m <parent-number> or –mainline <parent-number>
- Used for cherry-picking merge commits.
- Specifies which parent of the merge commit should be considered as the mainline to replay changes.
Example
git cherry-pick -m 1 <merge-commit-hash>
1
refers to the first parent of the merge commit.
This option is necessary for cherry-picking merge commits, as Git cannot automatically determine the base.
5. –cleanup=<mode>
Specifies how to clean up the commit message.
default
: Cleans up whitespace and comments.verbatim
: Uses the commit message exactly as it is.whitespace
: Removes unnecessary whitespace
Example
git cherry-pick --cleanup=verbatim <commit-hash>
6. -rerere-autoupdate
Automatically updates the index with the result of resolved conflicts, using the recorded resolutions if possible.
Example
git cherry-pick --rerere-autoupdate <commit-hash>
7. –no-gpg-sign
Disables GPG signing for the cherry-picked commit, even if GPG signing is enabled globally.
Example
git cherry-pick --no-gpg-sign <commit-hash>
8. –allow-empty-message
Allows cherry-picking of commits that have no commit message.
Example
git cherry-pick --allow-empty-message <commit-hash>
9. –allow-empty
Allows cherry-picking of commits that don’t introduce any changes (empty commits).
git cherry-pick --allow-empty <commit-hash>
10. -s or –signoff
Adds a “Signed-off-by” line at the end of the commit message.
This is commonly used for contributing to projects that follow the Developer Certificate of Origin (DCO)
Example
git cherry-pick -s <commit-hash>
11. –strategy-option=<option>
Adds options for the specified merge strategy.
For example, to resolve conflicts by automatically favoring the current branch
git cherry-pick --strategy-option=ours <commit-hash>
12. –strategy=<strategy>
Specifies the merge strategy to use when applying changes.Common strategies:
recursive
(default): Merges changes recursively.ours
: Keeps the current branch’s changes.theirs
: Uses the incoming changes
git cherry-pick --strategy=recursive <commit-hash>
13. –quite
Stops the cherry-pick process but leaves any changes made so far in the working directory.Use this if you want to pause the cherry-pick process without resetting everything.
Example:
git cherry-pick --quit
14. –abort
Cancels the cherry-pick process and resets your branch to the state before the cherry-pick began.
Example:
git cherry-pick --abort
15. –continue
Resumes the cherry-pick process after resolving conflicts.
Example:
git cherry-pick --continue
Summary of Options
Option | Description |
---|---|
-e / --edit | Edit the commit message before committing. |
-x | Append the original commit hash to the message. |
-n / --no-commit | Apply changes without committing. |
-m <parent> | Specify the parent for merge commits. |
--continue | Resume after resolving conflicts. |
--abort | Cancel the cherry-pick. |
--quit | Stop the process but keep changes. |
--strategy=<name> | Specify the merge strategy. |
--strategy-option | Provide additional options for the merge strategy. |
-s / --signoff | Add a “Signed-off-by” line to the commit message. |
--allow-empty | Cherry-pick empty commits. |
--allow-empty-message | Cherry-pick commits without a message. |
--no-gpg-sign | Disable GPG signing for the commit. |
--rerere-autoupdate | Automatically resolve conflicts using previously recorded resolutions. |
--cleanup=<mode> | Clean up commit messages based on the specified mode. |
Handling Conflicts:
If conflicts arise during a cherry-pick, Git will pause and allow you to resolve them. After resolving conflicts, you can continue the process with:
git cherry-pick --continue
To abort the cherry-pick and return to the state before it was initiated:
git cherry-pick --abort
Cherry-picking Multiple Commits
1. Specify Multiple Commit Hashes
You can list multiple commit hashes separated by spaces. For example:
git cherry-pick <commit-hash-1> <commit-hash-2> <commit-hash-3>
Git will sequentially apply each of these commits to the current branch. If any conflicts arise, Git will pause the process for you to resolve the conflicts.
2. Use a Range of Commits
You can cherry-pick a range of commits using the ..
syntax. For example:
git cherry-pick <start-commit-hash>..<end-commit-hash>
In this case:
<start-commit-hash>
is not included in the range.<end-commit-hash>
is included in the range.
To include both commits in the range, prepend ^
to the starting commit:
git cherry-pick <start-commit-hash>^..<end-commit-hash>
3. Cherry-picking Sequential Commits
If the commits you want to cherry-pick are sequential, you can use HEAD~n
syntax or branch references:
git cherry-pick A..B
Where:
- A is the older commit (not included unless using
A^
). - B is the newer commit (always included).
Options for Cherry-picking Multiple Commits
Edit Commit Messages: To edit commit messages for each commit during the cherry-pick process:
git cherry-pick -e <commit-hash-1> <commit-hash-2>
Append Original Commit Info: To include a reference to the original commit in the new commits:
git cherry-pick -x <commit-hash-1>..<commit-hash-3>
No Automatic Commit: To stage changes from all cherry-picked commits without committing them:
git cherry-pick -n <commit-hash-1>..<commit-hash-3>