LinuxCommandLibrary

git-cherry-pick

Apply specific commits from one branch to another

TLDR

Apply a commit to the current branch

$ git cherry-pick [commit]
copy

Apply a range of commits to the current branch (see also: git rebase --onto)
$ git cherry-pick [start_commit]~..[end_commit]
copy

Apply multiple (non-sequential) commits to the current branch
$ git cherry-pick [commit1 commit2 ...]
copy

Add the changes of a commit to the working directory, without creating a commit
$ git cherry-pick [[-n|--no-commit]] [commit]
copy

SYNOPSIS

git cherry-pick [options] <commit>... [--] [<pathspec>...]

PARAMETERS

-e, --edit
    Edit commit message before committing

-x
    Append '(cherry picked from )' to message

-m parent-number, --mainline parent-number
    For merge commits, indicate mainline parent (1 or 2)

--commit
    Apply and commit (default); inverse of --no-commit

-n, --no-commit
    Apply changes but do not commit

--no-empty
    Omit empty commits (default)

--ff
    Allow fast-forward non-conflict commits

-s, --signoff
    Add Signed-off-by trailer

-S[keyid], --gpg-sign[=keyid]
    GPG-sign commit

--strategy=<strategy>
    Merge strategy (e.g., recursive, ort)

-X <option>, --strategy-option=<option>
    Pass option to merge strategy

--allow-empty
    Allow creating empty commits

--abort
    Abort cherry-pick and reset changes

--continue
    Continue after resolving conflicts

--quit
    Abort but keep HEAD and index

DESCRIPTION

Git cherry-pick applies the changes introduced by one or more existing commits to the current branch, creating new commits with identical changes but different hashes. This is ideal for selectively porting bug fixes, features, or hotfixes from one branch to another without merging entire histories.

Executing git cherry-pick <commit> copies the commit's delta, applies it to the working tree, and commits it (unless --no-commit is used). For merge commits, specify the mainline parent with -m 1 or -m 2. If conflicts occur, Git stops for manual resolution; use git add to stage fixes, then git cherry-pick --continue, --abort, or --quit.

Key features include editing messages (-e), tracking origins (-x), fast-forwarding (--ff), custom merge strategies (--strategy), and GPG signing (-S). Multiple commits can be specified as a range, like git cherry-pick branch^..branch. It preserves metadata like authorship but duplicates changes, risking future conflicts.

CAVEATS

Cherry-picking duplicates commits, leading to non-linear history and potential future merge conflicts. Avoid on public/shared branches. Merge commits require -m; incorrect parent selection applies wrong changes. Does not handle submodules well without custom strategy.

EXAMPLES

git cherry-pick abc123 – Pick single commit.
git cherry-pick -x feature^..feature – Pick range, track origins.
git cherry-pick -n master – Stage changes without commit.

RESOLVING CONFLICTS

After conflict: edit files, git add <file>, git cherry-pick --continue. Use git cherry-pick --abort to cancel.

HISTORY

Introduced in Git 0.99.9c (2005) as an evolution of early patch application tools. Enhanced in Git 1.5+ with merge commit support (-m), strategies, and conflict handling. Git 2.29+ added ort strategy. Widely used for stable backports in Linux kernel development.

SEE ALSO

Copied to clipboard