Table of contents
- What is
git cherry-pick
? - When to use cherry-pick
- How to cherry-pick a commit
- Handling cherry-pick conflicts
- Example: Cherry-picking across branches
- Common pitfalls and tips
- Graphite CLI
- Summary
What is git cherry-pick
?
git cherry-pick
applies a specific commit from one branch onto another. Instead of merging an entire branch, you pick an individual commit and apply its changes on your current branch. It creates a new commit with a different SHA but the same content as the original.
This command is useful when you want just one commit's change without merging unrelated changes from its source branch.
When to use cherry-pick
Use git cherry-pick
for targeted scenarios like:
- Hotfixes: A bug is fixed on a feature branch, but the fix is needed immediately on
main
. Cherry-pick only that commit. - Backporting: You want to apply a fix to an older release branch without merging new features.
- Commit recovery: You closed a PR or lost a branch but want to recover a specific commit.
- Shared utility code: A helpful function lives on another branch and you want to reuse it without pulling in all changes.
If you find yourself cherry-picking more than a few commits or repeatedly, it's often a sign that rebasing or merging may be a better strategy.
How to cherry-pick a commit
Here's how to apply a single commit from one branch to another.
find the commit hash Use
git log --oneline <branch>
orgit log
to locate the commit you want. Copy the hash (e.g.,3a1b2c3
).checkout the target branch
Terminalgit checkout mainapply the commit
Terminalgit cherry-pick <commit-hash>Git will apply the change as a new commit on your current branch. If successful, it will auto-commit and advance the branch.
push the result
Terminalgit push origin main
If you're cherry-picking a range:
git cherry-pick A^..C
This applies commits A through C (inclusive), assuming they are linear.
Handling cherry-pick conflicts
Conflicts happen when changes in the cherry-picked commit overlap with changes in your current branch.
When Git reports a conflict:
check status
Terminalgit statusresolve conflicts manually Open files with conflict markers (
<<<<<<<
,=======
,>>>>>>>
) and edit them to keep the correct version.stage resolved files
Terminalgit add <file>continue cherry-pick
Terminalgit cherry-pick --continue
If you want to cancel:
git cherry-pick --abort
To skip a problematic commit in a range:
git cherry-pick --skip
Example: Cherry-picking across branches
Say your history looks like this:
main: A---B---C\feature: D---E
Commit E
on feature
contains a bug fix. To apply just E
to main
:
git checkout maingit cherry-pick <hash-of-E>
You now have:
main: A---B---C---E'
The commit E'
is a new commit with the same changes as E
, but a different SHA.
Common pitfalls and tips
Duplicate commits: If you cherry-pick a commit and later merge the source branch, you'll get duplicate changes. Use
git cherry-pick -x
to record the origin in the commit message for traceability.Missing dependencies: Some commits depend on earlier changes. Cherry-picking them alone might break builds or tests. Always review the context of the commit.
Too many cherry-picks: If you're cherry-picking entire sections of work, consider merging or rebasing instead. Cherry-picking is ideal for one-off commits.
Remote commits: To cherry-pick a commit from a remote branch, first fetch it:
Terminalgit fetch origin feature-xgit cherry-pick <commit-hash>Avoiding confusion: Communicate with your team if you cherry-pick. It's easy to confuse history if a commit appears in multiple branches with different hashes.
Graphite CLI
Graphite is a Git-centric tool that enhances workflows around stacked pull requests. Its CLI, commonly invoked with gt
, sits on top of vanilla Git and offers features like visual logs, branch stacking, automatic restacking, and simplified rebasing. It’s fully compatible with Git – your regular Git commands still work alongside it. To learn more about how you can use the Graphite CLI alongside Git, check out this documentation.
Summary
git cherry-pick
is a precision tool for applying individual commits across branches. It's perfect for urgent fixes, shared utilities, or backporting without merging unrelated work. Use it when the commit you want is self-contained and doesn't introduce dependencies.
That said, overuse can complicate history — especially if those cherry-picked commits eventually get merged too. Use sparingly, understand the context of what you're picking, and prefer merge or rebase for broader changes.