cherry-pick Git Command Guide
The git cherry-pick command applies changes introduced by existing commits to the current branch. It’s commonly used to incorporate bug fixes or features from other branches without performing a merge.
git cherry-pick Syntax:
Section titled “git cherry-pick Syntax:”git cherry-pick [--edit] [-n] [--no-commit] [-x] [-s] [-m <parent-number>] [-p] [-S[<keyid>]] [--signoff] [--gpg-sign[=<key-id>]] [--ff] [--allow-empty] [--allow-empty-message] [--keep-redundant-commits] [--no-affordance] <commit-ish>...git cherry-pick --continuegit cherry-pick --skipgit cherry-pick --abortgit cherry-pick --quitOptions:
Section titled “Options:”| Option | Description |
|---|---|
| —continue | Continue the operation in progress using the information in .git/sequencer |
| —skip | Skip the current commit and continue with the rest of the sequence |
| —abort | Cancel the operation and return to the pre-sequence state |
| —quit | Forget about the current operation in progress and return to the pre-sequence state |
| -e, —edit | Edit the commit message prior to committing |
| -n, —no-commit | Apply the changes necessary to cherry-pick but do not make any commit |
| -x | When recording the commit, append a line that says “(cherry picked from commit …)” to the original commit message |
| -s, —signoff | Add a Signed-off-by trailer at the end of the commit message |
| —gpg-sign[= | GPG-sign commits (default is the current user) |
| —no-gpg-sign | Don’t GPG-sign commits |
| -m | Use the nth parent of the commit as the mainline |
| —mainline | Use the nth parent of the commit as the mainline |
| -p, —strategy-option=recursive,ours | Use a merge strategy |
| —strategy= | Use a given merge strategy |
| —rerere-autoupdate | Allow the rerere mechanism to update the index |
| —no-rerere-autoupdate | Do not allow the rerere mechanism to update the index |
| —allow-empty | Preserve potentially empty commits |
| —allow-empty-message | Allow commits with empty messages |
| —keep-redundant-commits | Create redundant, empty commits even without —allow-empty |
| —no-affordance | Verbose output showing skipped commits |
Parameters:
Section titled “Parameters:”| Parameter | Description |
|---|---|
| Commit to cherry-pick. Can be SHA-1, branch name, tag, etc. |
git cherry-pick Command Samples:
Section titled “git cherry-pick Command Samples:”Simple cherry-pick of a single commit
Section titled “Simple cherry-pick of a single commit”git cherry-pick 4c3f2b1Applies the changes from commit 4c3f2b1 to the current branch.
Cherry-pick multiple commits
Section titled “Cherry-pick multiple commits”git cherry-pick 1a2b3c4 5d6e7f8Applies changes from the two specified commits.
Cherry-pick with commit message edit
Section titled “Cherry-pick with commit message edit”git cherry-pick --edit 9h0i1j2Allows editing the commit message before applying.
Cherry-pick without committing (for staging)
Section titled “Cherry-pick without committing (for staging)”git cherry-pick -n 2k3l4m5Applies the changes but doesn’t create the commit, allowing you to review or combine with other changes.
Cherry-pick from another branch
Section titled “Cherry-pick from another branch”git cherry-pick feature-branch~2Applies the commit two steps back from the tip of feature-branch.
Handle merge conflicts and continue
Section titled “Handle merge conflicts and continue”git cherry-pick --continueAfter resolving conflicts, continue the cherry-pick operation.
Skip a commit during multi-commit cherry-pick
Section titled “Skip a commit during multi-commit cherry-pick”git cherry-pick --skipSkips the current commit if it can’t be applied cleanly.
Abort a cherry-pick operation
Section titled “Abort a cherry-pick operation”git cherry-pick --abortCancels the entire cherry-pick sequence and returns to the original state.
Add cherry-pick tracking information
Section titled “Add cherry-pick tracking information”git cherry-pick -x upstream-fixAppends cherry picked from commit... to the commit message.
How do I cherry-pick a commit from another branch?
Section titled “How do I cherry-pick a commit from another branch?”To cherry-pick a commit from another branch, use:
git cherry-pick <commit-hash>How can I cherry-pick multiple commits at once?
Section titled “How can I cherry-pick multiple commits at once?”To cherry-pick multiple commits at once, execute:
git cherry-pick <first-commit> <second-commit>How do I handle merge conflicts during cherry-pick?
Section titled “How do I handle merge conflicts during cherry-pick?”To handle merge conflicts during cherry-pick, resolve the conflicts in the affected files, then run:
git cherry-pick --continueHow can I edit the commit message when cherry-picking?
Section titled “How can I edit the commit message when cherry-picking?”To edit the commit message when cherry-picking, use:
git cherry-pick --edit <commit-hash>How do I skip a commit during multi-commit cherry-pick?
Section titled “How do I skip a commit during multi-commit cherry-pick?”To skip a commit during multi-commit cherry-pick, use:
git cherry-pick --skipApplications of the git cherry-pick command
Section titled “Applications of the git cherry-pick command”- Backporting bug fixes from development branches to release branches
- Applying selective commits from feature branches to master
- Reordering commits or fixing commit history locally
- Creating patch releases with specific fixes
- Incorporating upstream changes in forked repositories without full merges
- Selective integration of commits from long-lived topic branches