Cherry-Picking

The git cherry-pick command allows you to apply one or more commits from any other branch in the repository to the HEAD of the current branch. Cherry-picking is useful when you do not wish to apply all commits on a source branch to the current branch. If you aim to merge all commits from a source branch into the current branch, you should instead use Merge or Rebase.

In the example below, we want to cherry-pick commit C from the branch a-branch into the HEAD of the current (target) main branch.

This will append a new commit C', equivalent to C, onto the main branch.

  1. Before main points to D; C lies on a-branch.
    main points to D; C lies on a-branch.
  2. Git commands

    git cherry-pick selected
    
  3. After main has a new copy of C after D; a-branch retains the original C.
    main has a new copy of C after D; a-branch retains the original C.
Recreate this repository

Run these commands in a fresh directory.

git init -b main
echo Example repository > README.md
git add README.md
git commit -m "..."
git switch -c a-branch
echo C > c.txt
git add c.txt
git commit -m "C"
git tag selected
echo More work on a-branch > continuation.txt
git add continuation.txt
git commit -m "..."
git switch main
echo A > a.txt
git add a.txt
git commit -m "A"
echo B > b.txt
git add b.txt
git commit -m "B"
echo D > d.txt
git add d.txt
git commit -m "D"

Note

Cherry-picking commits should be done carefully. In the above example, after cherry-picking C, a future attempt to merge a-branch into main could result in merge-conflicts because C and C' may conflict. If a conflict occurs during cherry-picking, the working tree will enter ‘Cherry-Picking’ status. You’ll either need to resolve the conflict (e.g., using the SmartGit Conflict Resolver) and complete the cherry-pick or abort the cherry-pick attempt.

See also

Cherry Pick in SmartGit