π 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:
- Get the commit hash from the bugfix branch:
git checkout bugfix
git log --oneline
Example output:
a1b2c3d Fix: resolved login error
- Switch to the target branch:
git checkout release
- 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:
- Open the file and fix the conflicts.
- Mark it as resolved:
git add file.txt
- 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
Command | Purpose |
---|---|
git cherry-pick <hash> | Apply a single commit to current branch |
git cherry-pick A^..C | Apply range of commits A to C |
git cherry-pick --continue | Complete cherry-pick after resolving conflicts |
git cherry-pick --abort | Abort 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 :