← Back to Git Course | Chapter 3: Branching | Lesson 7 of 9

Git cherry-pick Command

Introduction to Cherry-Pick

git cherry-pick <commit> copies the exact changes from one specific commit on another branch and applies them as a new commit on your current branch -- useful for pulling in a single bug fix without merging that branch's other unrelated commits.

Example: Introduction to Cherry-Pick

bash
git cherry-pick a1b2c3d

Cherry-Picking a Range of Commits

git cherry-pick <commit1>^..<commit2> applies a whole contiguous range of commits in order, rather than picking them one at a time, which is faster when you need several related commits from another branch.

Example: Cherry-Picking a Range of Commits

bash
git cherry-pick a1b2c3d^..e4f5g6h

Handling Cherry-Pick Conflicts

If a cherry-picked commit conflicts with your current branch's code, the process pauses just like a merge conflict would -- you can resolve the conflict and continue, or run git cherry-pick --abort to cancel and return to where you started.

Example: Handling Cherry-Pick Conflicts

bash
git cherry-pick a1b2c3d
# fix conflicts, then:
git add file.txt
git cherry-pick --continue
# or: git cherry-pick --abort

Cherry-Picking Merge Commits

Merge commits have two parents, so cherry-picking one is ambiguous by default -- the -m flag tells Git which parent's line of changes to treat as the mainline when computing the diff to apply.

Example: Cherry-Picking Merge Commits

bash
git cherry-pick -m 1 a1b2c3d

Skipping Conflicts

git cherry-pick --skip abandons just the currently-conflicting commit and moves on to the next one in a multi-commit pick, while git cherry-pick --abort resets everything back to before the cherry-pick began.

Example: Skipping Conflicts

bash
git cherry-pick --skip
git cherry-pick --abort

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.