Skip to content

Drill: Cherry-Pick Specific Commits Between Branches

Goal

Use git cherry-pick to selectively apply specific commits from one branch to another without merging all changes.

Setup

  • A git repository with at least two branches
  • Commits on one branch that need to be applied to another

Commands

Set up a practice scenario:

mkdir /tmp/cherry-drill && cd /tmp/cherry-drill && git init
echo "base" > file.txt && git add file.txt && git commit -m "base"
git checkout -b feature
echo "feature-1" >> file.txt && git add file.txt && git commit -m "feature: add part 1"
echo "feature-2" >> file.txt && git add file.txt && git commit -m "feature: add part 2"
echo "hotfix" >> file.txt && git add file.txt && git commit -m "fix: critical bugfix"
echo "feature-3" >> file.txt && git add file.txt && git commit -m "feature: add part 3"

Find the commit to cherry-pick:

git log --oneline feature

Switch to main and cherry-pick the bugfix:

git checkout main
git cherry-pick <commit-hash-of-bugfix>

Cherry-pick multiple commits:

git cherry-pick <hash1> <hash2>

Cherry-pick a range of commits:

git cherry-pick <start-hash>..<end-hash>

Cherry-pick without committing (stage only):

git cherry-pick --no-commit <hash>

If there is a conflict, resolve and continue:

# Edit conflicting files
git add <resolved-files>
git cherry-pick --continue

Abort a cherry-pick in progress:

git cherry-pick --abort

What to Look For

  • Cherry-picked commits get new hashes (they are new commits with the same diff)
  • Conflicts arise when the cherry-picked change touches code that differs between branches
  • --no-commit lets you combine multiple cherry-picks into one commit
  • git log --cherry-mark shows which commits have been cherry-picked between branches

Common Mistakes

  • Cherry-picking a commit that depends on earlier commits not present on the target branch
  • Forgetting that cherry-pick creates duplicate commits (later merge may show conflicts)
  • Not using --no-commit when you want to squash multiple picks into one
  • Cherry-picking merge commits without specifying -m 1 for the mainline parent

Cleanup

rm -rf /tmp/cherry-drill