Git Cherry Pick: Apply Commits Across Branches
- 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 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.
# 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"
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 -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
(cherry picked from commit a3f9c2e)
| Scenario | Use Cherry Pick? | Better Alternative |
|---|---|---|
| Backport a security fix to a release branch | Yes | Cherry pick with -x |
| Apply a single bug fix to a hotfix branch | Yes | Cherry pick with -x |
| Bring all changes from feature branch to main | No | git merge or git rebase |
| Apply a commit from a colleague's WIP branch | Sometimes | Wait for their PR; cherry pick in emergencies |
| Undo a specific commit on another branch | No | git 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.
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.