Fundamentally, what git cherry-pick
does is to convert each "cherry" (each commit to be picked) into a changeset—a set of diffs—and then attempt to apply that changeset to your current (or HEAD
) commit1 and make a new commit from the result.
The diff for any one particular commit is obtained by running git diff
between that commit and its parent commit. This diff is easy to obtain: git show <hash>
does it for any non-merge commit. (Cherry-picking a merge requires specifying which parent and is therefore a bit more complex.) Thus, you can use git show
to view what changes git cherry-pick
will pick.
Note that if git show
shows a change to a file that does not exist in HEAD
, or to a region of code that does not exist in a file that does exist in HEAD
, Git will be unable to complete the cherry-pick on its own. In this case, Git will treat the cherry-pick operation as a request to merge the changes, using a full 3-way merge, with the merge base being the parent of the commit being cherry-picked and the two branch tips being HEAD
(--ours
) and the commit being cherry-picked (--theirs
). This will leave the merge conflict in the work-tree, with the in-progress merge stored in the index; you resolve this as you would any other merge conflict, then run git cherry-pick --continue
to finish the operation, or git cherry-pick --abort
to stop and go back to the way things were before you started.
1Or, with -n
, to the index and work-tree only.