Skip to main content
Cherry-picking allows you to apply the changes introduced by existing commits to your current branch. This is useful when you need specific changes from another branch without merging the entire branch.

What is Cherry-Pick?

Cherry-picking creates a new commit on your current branch with the same changes as an existing commit from another branch. The new commit has a different hash but the same diff.
Before cherry-pick:          After cherry-pick:

A---B---C---D  main          A---B---C---D---E' main
     \
      E---F  feature              E---F  feature
Commit E’ contains the same changes as E but is a new commit on main.

Basic Cherry-Pick

Applying a Single Commit

To apply a specific commit to your current branch:
git cherry-pick <commit-hash>
Example:
# On the main branch
git cherry-pick a1b2c3d
This applies the changes from commit a1b2c3d and creates a new commit on main.

Applying Multiple Commits

To cherry-pick a range of commits:
# Apply commits from A to B (exclusive of A, inclusive of B)
git cherry-pick A..B

# Apply commits from A to B (inclusive of both)
git cherry-pick A^..B
Example:
# Cherry-pick the last 3 commits from feature branch
git cherry-pick feature~2..feature

Cherry-Pick Options

Edit the Commit Message

Modify the commit message before applying:
git cherry-pick -e <commit>
or
git cherry-pick --edit <commit>

Record the Origin

Add a “cherry picked from” line to the commit message:
git cherry-pick -x <commit>
This appends:
(cherry picked from commit a1b2c3d4e5f...)
Use -x when cherry-picking between public branches (like backporting a fix). Don’t use it for private branches as the information is not useful to others.

Apply Without Committing

Apply the changes but don’t create a commit:
git cherry-pick -n <commit>
or
git cherry-pick --no-commit <commit>
Useful when cherry-picking multiple commits to combine into one.

Sign Off the Commit

Add a Signed-off-by trailer:
git cherry-pick -s <commit>

Handling Conflicts

When cherry-picking causes conflicts, Git pauses and marks the conflicts:
1
Review the conflicts
2
git status
3
Conflicted files are marked as “both modified.”
4
Resolve conflicts
5
Edit the conflicted files and remove conflict markers:
6
<<<<<<< HEAD
Current branch content
=======
Cherry-picked content
>>>>>>> commit-hash
7
Mark as resolved
8
git add <resolved-files>
9
Continue the cherry-pick
10
git cherry-pick --continue

Aborting a Cherry-Pick

To cancel the cherry-pick and return to the pre-cherry-pick state:
git cherry-pick --abort

Skipping a Commit

When cherry-picking multiple commits, skip the current one:
git cherry-pick --skip

Cherry-Picking Merge Commits

Merge commits have multiple parents, so you must specify which parent to use:
git cherry-pick -m 1 <merge-commit>
  • -m 1 uses the first parent (usually the branch you merged into)
  • -m 2 uses the second parent (usually the branch being merged)
Example:
# Cherry-pick a merge commit, using the main branch as mainline
git cherry-pick -m 1 abc123

Common Use Cases

Backporting a Bug Fix

Apply a fix from main to a release branch:
git checkout release-1.0
git cherry-pick -x abc123
git push origin release-1.0

Moving a Commit to the Wrong Branch

If you committed to the wrong branch:
  1. Note the commit hash: git log
  2. Switch to the correct branch: git checkout correct-branch
  3. Cherry-pick the commit: git cherry-pick abc123
  4. Switch back: git checkout wrong-branch
  5. Remove the commit: git reset --hard HEAD^

Applying Specific Features

Cherry-pick feature commits from a development branch:
# Cherry-pick only the authentication feature commits
git cherry-pick feature~5..feature~3

Testing a Fix Before Merging

# Test a potential fix without merging the entire branch
git cherry-pick -n potential-fix
make test

# If tests pass, commit it
git commit -m "Apply fix from potential-fix branch"

Advanced Examples

Cherry-Pick with Strategy Options

Use merge strategies when cherry-picking:
# Use patience diff algorithm for better conflict resolution
git cherry-pick -Xpatience abc123

# Prefer changes from the cherry-picked commit
git cherry-pick -Xtheirs abc123

Cherry-Pick Multiple Non-Consecutive Commits

git cherry-pick abc123 def456 ghi789

Cherry-Pick and Combine Commits

# Apply multiple commits without committing
git cherry-pick -n commit1 commit2 commit3

# Review the combined changes
git diff --staged

# Commit as one
git commit -m "Combined feature from commits 1, 2, and 3"

Programmatic Cherry-Picking

Cherry-pick commits matching a pattern:
# Find all commits that touched README and cherry-pick them
git rev-list --reverse main -- README | \
  git cherry-pick -n --stdin

Cherry-Pick vs. Rebase vs. Merge

OperationUse CaseHistory Impact
Cherry-pickApply specific commitsDuplicates commits
RebaseUpdate feature branchRewrites history
MergeIntegrate branchesPreserves history

Best Practices

  1. Use -x for public branches - Always record the source when backporting fixes to release branches
  2. Prefer merging over cherry-picking - Cherry-picking duplicates commits. When possible, merge branches instead
  3. Test after cherry-picking - The same changes may behave differently in different contexts
  4. Document why you cherry-picked - Use clear commit messages explaining why this specific commit was cherry-picked
  5. Avoid cherry-picking many commits - If you need many commits from a branch, consider merging instead
  6. Be careful with dependencies - Ensure all dependencies are present when cherry-picking individual commits

Troubleshooting

Empty Commit After Cherry-Pick

If the changes are already present in your branch:
# Allow empty commits
git cherry-pick --allow-empty abc123

# Or skip the empty commit
git cherry-pick --skip

Cherry-Pick Applies Wrong Changes

Revert the cherry-pick:
# Undo the last commit
git reset --hard HEAD^

# Or revert it with a new commit
git revert HEAD

Finding Commits to Cherry-Pick

# Show commits in feature but not in main
git log main..feature --oneline

# Show commits that modified specific files
git log feature -- path/to/file

Configuration Options

Set default cherry-pick behavior:
[alias]
    # Cherry-pick with automatic sign-off and origin recording
    cp = cherry-pick -x -s