Class 37 of 50 • Advance Git Internals

Git Cherry-Pick: Applying Isolated Commits Across Independent Branches

Imagine you are working on an experimental feature branch with 30 commits that won't be ready for production for months. While coding, you discovered and fixed a critical security bug in commit 7d1935a. The production team needs that bugfix on main right now, but they cannot merge your unfinished feature branch. Git Cherry-Pick allows you to pluck that single commit out and apply it directly onto main.

Basic Syntax and Execution

# Step 1: Switch to the destination branch (where you want the commit to land):
$ git switch main

# Step 2: Cherry-pick the target commit hash:
$ git cherry-pick 7d1935a
[main 4f8e2a1] fix(security): sanitize user input against SQL injection
 Date: Sun Nov 8 07:00:00 2026 +0000
 1 file changed, 8 insertions(+), 2 deletions(-)

Notice that Git created a brand-new commit on main with a new hash (4f8e2a1), but preserved the exact diff, author information, and commit message from 7d1935a!

Advanced Cherry-Picking Techniques

1. Cherry-Picking Multiple Commits

# Cherry-pick three distinct commits:
$ git cherry-pick 4a1b2c 8d9e0f 3f2a1b

# Cherry-pick a contiguous range of commits (from A to B, exclusive of A):
$ git cherry-pick A..B

# Cherry-pick a contiguous range INCLUSIVE of A:
$ git cherry-pick A^..B

2. Cherry-Picking Without Committing (-n / --no-commit)

If you want to apply the changes to your working directory and staging area without automatically generating a commit:

$ git cherry-pick -n 7d1935a

Handling Conflicts During Cherry-Pick

If the cherry-picked commit touches code that differs on the destination branch, Git pauses and triggers a conflict:

  1. Open the conflicting file and resolve the conflict markers manually.
  2. Stage the resolved file: git add [file].
  3. Continue the cherry-pick: git cherry-pick --continue (do NOT run git commit!).
  4. To abort and return to clean state: git cherry-pick --abort.

Explore Related Git & GitHub Tutorials & Next Steps

Strengthen your version control mastery by studying these connected classes:

Rebase vs Cherry-Pick

Compare single commit cherry-picks with full multi-commit branch rebasing.

Commit Objects & Hashes

Understand how cherry-picking creates a new commit with identical diffs.

Resolving Conflicts During Cherry-Pick

Resolve merge conflicts and advance cherry-picks with git cherry-pick --continue.

Hands-on Lab Practice Scenarios

Cherry-pick a critical bugfix from develop into an urgent production release.

Frequently Asked Questions (FAQ) - Student & Interview Review

No! Cherry-pick copies the diff and creates a duplicate new commit on the target branch. The original commit remains completely intact on the source branch.
Cherry-picking creates duplicate commits with different hashes. If both branches are merged later, Git may encounter redundant diffs. Use cherry-picking for urgent hotfixes and backports, not as a replacement for standard branching and merging.
Pass the `-x` flag: `git cherry-pick -x [hash]`. Git appends a line to the commit message: `(cherry picked from commit 7d1935a...)` for traceability.

Next Step in Your Git & GitHub Learning Path:

Advance your software engineering skills with the next topic in our synchronized curriculum.

Continue to git reflog Recovery → Previous: ← Refs, HEAD & Symrefs | Full Syllabus

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Reddit

You may also like this!