πŸ“š Advanced Git & GitHub Topics
Estimated reading: 5 minutes 15 views

πŸ’ Git Cherry-pick – Apply Specific Commits Across Branches with Precision


🧲 Introduction – Why Use Git Cherry-pick?

Ever worked on a feature branch and made a perfect commit that you’d also like on another branchβ€”without merging the whole branch? That’s exactly what git cherry-pick does. It lets you apply a specific commit from one branch onto another, selectively and surgically.

🎯 In this guide, you’ll learn:

  • What git cherry-pick is
  • How to use it with examples
  • Common issues and how to resolve them
  • Best practices for safe and clean cherry-picking

πŸ” What Is git cherry-pick?

git cherry-pick is a Git command that allows you to apply changes from a specific commit (or a series of commits) onto your current branch. Unlike merge or rebase, which bring in complete branch histories, cherry-pick only transfers the chosen commit(s).


🧰 git cherry-pick? Syntax and Basic Usage

πŸ”€ git cherry-pick Basic Syntax:

git cherry-pick <commit-hash>

πŸ“Ž git cherry-pick Example:

Let’s say you’re on the main branch, and you want to apply a commit from feature:

git checkout main
git cherry-pick a1b2c3d

βœ… This applies commit a1b2c3d from the feature branch onto main.


πŸ§ͺ Practical Example – Step-by-Step

πŸ”§ Scenario:

  • You’ve fixed a bug in the bugfix branch.
  • That fix is also needed on the release branch.

βœ… Steps:

  1. Get the commit hash from the bugfix branch:
git checkout bugfix
git log --oneline

Example output:

a1b2c3d Fix: resolved login error
  1. Switch to the target branch:
git checkout release
  1. Apply the commit using cherry-pick:
git cherry-pick a1b2c3d

πŸŽ‰ Done! The commit is now part of the release branch as if it was originally created there.


πŸ” Cherry-pick Multiple Commits

You can cherry-pick a range of commits too:

git cherry-pick <start-commit>^..<end-commit>

Example:

git cherry-pick a1b2c3d^..d4e5f6g

This applies all commits between a1b2c3d and d4e5f6g, inclusive.


🧠 Cherry-pick with Conflict Resolution

Sometimes, cherry-picking causes merge conflicts if the target branch has diverging changes.

Example:

git cherry-pick <commit-hash>
# Output: CONFLICT (content): Merge conflict in file.txt

βœ… Resolve it:

  1. Open the file and fix the conflicts.
  2. Mark it as resolved:
git add file.txt
  1. Complete the cherry-pick:
git cherry-pick --continue

🚫 Abort a Failed Cherry-pick

If things go wrong or you’re unsure:

git cherry-pick --abort

This reverts the working directory to the state before the cherry-pick began.


⚠️ Common Mistakes to Avoid

  • ❌ Cherry-picking large chains of commits instead of merging when appropriate.
  • ❌ Not resolving conflicts properly.
  • ❌ Using cherry-pick in team-shared branches without coordination (rewriting history risk).
  • ❌ Forgetting to test after cherry-picking β€” it may apply code out of context.

βœ… Best Practices for Git Cherry-pick

  • Always check the commit context before cherry-picking.
  • Use it for backporting fixes or hotfixes to release branches.
  • Avoid cherry-picking merges (git cherry-pick -m is required for that).
  • Don’t cherry-pick commits that rely heavily on previous ones unless you include the full range.

🧩 GUI Alternatives for Cherry-pick

  • VS Code Git Extension – Right-click commit β†’ Cherry-pick
  • GitKraken – Drag and drop a commit between branches
  • Sourcetree – Right-click β†’ Cherry-pick from log view

These visual tools help prevent mistakes, especially when selecting multiple commits.


πŸ“Š Summary Table – Git Cherry-pick Options

CommandPurpose
git cherry-pick <hash>Apply a single commit to current branch
git cherry-pick A^..CApply range of commits A to C
git cherry-pick --continueComplete cherry-pick after resolving conflicts
git cherry-pick --abortAbort ongoing cherry-pick
git cherry-pick -n <hash>Apply commit without auto-commit

πŸ“Œ Summary – Git Cherry-pick Options

git cherry-pick gives you precise control over what commits are brought into your current branch, making it ideal for hotfixes, patching, and selective updates. While powerful, it must be used carefully to avoid code inconsistencies and history confusion.

πŸ” Key Takeaways:

  • Use cherry-pick for applying specific commits across branches.
  • Always check for conflicts and test after cherry-picking.
  • Know when to prefer merge or rebase instead.

βš™οΈ Real-World Relevance: Teams often use git cherry-pick to backport bug fixes into older release branches or to apply emergency patches without merging unrelated features.


❓ Git Cherry-pick β€” Frequently Asked Questions (FAQs)

❓ Can I cherry-pick from a different branch without checking it out?
βœ… Yes. Use the format:

git cherry-pick branch_name~2

This cherry-picks the 3rd most recent commit from that branch.


❓ How do I cherry-pick a commit without committing it immediately?
βœ… Use the -n or --no-commit flag:

git cherry-pick -n <commit-hash>

This stages the changes without committing, so you can modify them first.


❓ What happens if I cherry-pick a merge commit?
βœ… You need to specify the parent branch using -m:

git cherry-pick -m 1 <merge-commit-hash>

But cherry-picking merge commits is advanced and usually discouraged.


❓ Can I cherry-pick multiple non-consecutive commits?
βœ… Yes, list them one after another:

git cherry-pick <hash1> <hash2> <hash3>

❓ How do I undo a cherry-pick?
βœ… If not yet pushed, use:

git reset --hard HEAD~1

If already pushed, revert the cherry-picked commit using:

git revert <cherry-picked-commit>

Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

Git Cherry-pick

Or Copy Link

CONTENTS
Scroll to Top