Homeβ€Ί DevOpsβ€Ί Git Cherry Pick: Apply Commits Across Branches

Git Cherry Pick: Apply Commits Across Branches

Where developers are forged. Β· Structured learning Β· Free forever.
πŸ“ Part of: Git β†’ Topic 16 of 19
Learn how to use git cherry-pick to apply specific commits from one branch to another.
βš™οΈ Intermediate β€” basic DevOps knowledge assumed
In this tutorial, you'll learn:
  • git cherry-pick -x <hash> applies a specific commit to your current branch and records the source hash in the message β€” always use -x for traceability.
  • Cherry-pick conflicts resolve the same way as merge conflicts: fix the files, git add, then git cherry-pick --continue.
  • Never cherry-pick merge commits without understanding -m β€” cherry-pick the individual commits inside the merge instead.
✦ Plain-English analogy ✦ Real code with output ✦ Interview questions
⚑ Quick Answer
Cherry picking in Git means 'I want exactly that one commit from that other branch, applied here.' You don't take the whole branch β€” just the specific change you need. It's the scalpel to git merge's broad brush. Most useful for backporting a critical fix to a release branch without dragging along every other change made since the release.

Cherry pick has a reputation as a footgun, and it deserves some of it. Applied carelessly on a long-lived project it creates duplicate commits that confuse git log and make merge histories unreadable. Applied precisely β€” a security fix backported to three release branches, a config change needed on a hotfix branch β€” it's the right tool and the alternatives are worse.

I've backported fixes to five simultaneous release branches using cherry-pick. The key discipline: always use -x so the cherry-picked commit records where it came from, and never cherry-pick a merge commit unless you know exactly what you're doing with the -m flag.

Basic Cherry Pick Usage

Find the hash of the commit you want, check out the destination branch, run git cherry-pick <hash>. Git applies the diff from that specific commit onto your current branch as a new commit.

The -x flag appends (cherry picked from commit <hash>) to the commit message. This traceability is not optional for any team maintaining release branches β€” without it you lose the ability to reason about which fixes have been applied where.

git_cherry_pick.sh Β· BASH
12345678910111213141516171819
# Find the commit hash to cherry pick
git log --oneline feature/security-fix
# a3f9c2e Fix SQL injection in payment query
# 7b2d4f1 Add input validation

# Cherry pick to release branch with traceability flag
git checkout release/2.4
git cherry-pick -x a3f9c2e

# Cherry pick a range of commits (inclusive on both ends)
git cherry-pick -x a3f9c2e..d4f8b3c

# Cherry pick from another branch by name (latest commit on that branch)
git cherry-pick -x feature/security-fix

# Cherry pick without committing (stages changes only)
git cherry-pick --no-commit a3f9c2e
git status   # Review staged changes
git commit -m "Backport: Fix SQL injection in payment query"
β–Ά Output
[release/2.4 b5c9d3f] Fix SQL injection in payment query
Date: Mon Mar 30 14:22:00 2026 +0530
1 file changed, 3 insertions(+), 1 deletion(-)
(cherry picked from commit a3f9c2e)

Handling Cherry Pick Conflicts

If the cherry-picked commit touches code that has diverged significantly on the destination branch, you'll get a merge conflict. Git stops mid-pick and leaves you in a CHERRY-PICKING state β€” visible in your shell prompt if you use a Git-aware prompt.

Resolve the conflict files, git add them, then git cherry-pick --continue. If you change your mind, git cherry-pick --abort restores the branch to its pre-pick state.

git_cherry_pick_conflict.sh Β· BASH
123456789101112131415161718192021
git cherry-pick -x a3f9c2e
# CONFLICT (content): Merge conflict in
# src/main/java/io/thecodeforge/payment/PaymentQueryBuilder.java

git status
# You are currently cherry-picking commit a3f9c2e.

# Edit conflicted file and resolve
# ... fix conflicts in PaymentQueryBuilder.java ...

git add src/main/java/io/thecodeforge/payment/PaymentQueryBuilder.java
git cherry-pick --continue
# Opens editor for commit message β€” keep or modify

# OR abort and return to clean state
git cherry-pick --abort

# Cherry picking multiple commits β€” if one fails, resolve and continue
git cherry-pick -x a3f9c2e 7b2d4f1 9c3e8a2
# Conflict on 7b2d4f1...
# Resolve, add, continue β€” applies the remaining commits in sequence
β–Ά Output
[release/2.4 c7d2e5f] Fix SQL injection in payment query
(cherry picked from commit a3f9c2e)
⚠️
Cherry picking merge commits requires -mIf you try to cherry-pick a merge commit, Git doesn't know which parent to diff against and will error: 'is a merge but no -m option was given'. The -m flag specifies which parent (1 for the main branch, 2 for the merged branch). In practice, cherry-picking merge commits almost always means you actually want to cherry-pick the individual commits within the merge. Cherry-picking individual feature commits is almost always the cleaner approach.
ScenarioUse Cherry Pick?Better Alternative
Backport a security fix to a release branchYesCherry pick with -x
Apply a single bug fix to a hotfix branchYesCherry pick with -x
Bring all changes from feature branch to mainNogit merge or git rebase
Apply a commit from a colleague's WIP branchSometimesWait for their PR; cherry pick in emergencies
Undo a specific commit on another branchNogit revert on that branch directly

🎯 Key Takeaways

  • git cherry-pick -x <hash> applies a specific commit to your current branch and records the source hash in the message β€” always use -x for traceability.
  • Cherry-pick conflicts resolve the same way as merge conflicts: fix the files, git add, then git cherry-pick --continue.
  • Never cherry-pick merge commits without understanding -m β€” cherry-pick the individual commits inside the merge instead.
  • Cherry pick is the right tool for backporting fixes to release branches. It's the wrong tool for keeping long-lived branches in sync β€” use merge or rebase for that.

⚠ Common Mistakes to Avoid

  • βœ•Not using -x flag β€” you lose traceability and in three months no one knows which release branches got the fix.
  • βœ•Cherry picking merge commits without understanding -m β€” almost always you want the individual commits inside the merge, not the merge commit itself.
  • βœ•Cherry picking to avoid a proper merge β€” if you're regularly cherry picking large amounts of work between long-lived branches, your branching strategy is the real problem.
  • βœ•Cherry picking the same commit to multiple branches and then merging those branches β€” creates duplicate commits in history that confuse git log and git blame.

Interview Questions on This Topic

  • QWhen would you use git cherry-pick instead of git merge?
  • QYou need to backport a security fix to three release branches. Walk me through the process using cherry-pick.
  • QWhat happens if you git cherry-pick a commit that has already been applied to the destination branch?

Frequently Asked Questions

What does git cherry-pick do?

git cherry-pick applies the changes introduced by a specific commit onto your current branch as a new commit. It copies the diff from the source commit β€” not the commit itself β€” so the new commit has a different hash. It's used to apply specific fixes or features from one branch to another without merging entire branches.

What is the -x flag in git cherry-pick?

The -x flag appends '(cherry picked from commit <hash>)' to the new commit's message. This creates a traceable link back to the source commit, which is essential when maintaining multiple release branches so you can quickly see which fixes have been applied where.

How do I cherry-pick a range of commits?

Use git cherry-pick A..B to apply commits after A up to and including B. Note this is exclusive of A β€” to include A, use git cherry-pick A^..B. For a list of specific non-consecutive commits, just list the hashes: git cherry-pick hash1 hash2 hash3.

πŸ”₯
Naren Founder & Author

Developer and founder of TheCodeForge. I built this site because I was tired of tutorials that explain what to type without explaining why it works. Every article here is written to make concepts actually click.

← PreviousGit Stash: Save and Restore Work in ProgressNext β†’Git Reset: Hard, Soft and Mixed Explained
Forged with πŸ”₯ at TheCodeForge.io β€” Where Developers Are Forged