Cherry-Picking Commits and Finding What's Missing Between Branches
Copy individual commits between branches with git cherry-pick: single commits, ranges, recording the source with -x, conflicts and empty picks, picking without committing, merge commits, and the tools that find commits one branch lacks: git cherry, --cherry-pick, rev-list and range-diff.
kvi5hnuCode Sprout team member7 min readOn this page
Sometimes you need one commit from another branch, not the whole branch: a bug fix made on develop that must also go into the release branch, or a useful commit stuck on an abandoned experiment. git cherry-pick copies the changes of chosen commits onto the current branch, as new commits. This article also covers the reverse question: which commits does one branch have that another lacks?
Picking one commit
git init -q trail-guide && cd trail-guide
echo "# Trail guide" > README.md; git add .; git commit -q -m "Start project"
git switch -q -c develop
echo "Lake path: 5 km" > lake.txt; git add .; git commit -q -m "Add lake page"
sed -i 's/guide/Guide/' README.md; git commit -q -am "Fix title capitalization"
echo "Forest walk: 3 km" > forest.txt; git add .; git commit -q -m "Add forest page"
git switch -q main
fix=$(git log --format=%h -1 --grep="Fix title" develop)
git cherry-pick -x $fix
echo "=== main now"
git log --oneline --graph --all --format='%h %s%d' --decorate
echo "=== the new commit's message"
git log -1 --format=%B
[main cbd648c] Fix title capitalization
Date: Mon Mar 2 10:00:00 2026 +0000
1 file changed, 1 insertion(+), 1 deletion(-)
=== main now
* 9a01331 Add forest page (develop)
* 670a756 Fix title capitalization
* d2a3ed1 Add lake page
| * cbd648c Fix title capitalization (HEAD -> main)
|/
* 7fb62a0 Start project
=== the new commit's message
Fix title capitalization
(cherry picked from commit 670a756286a6730bda2546b3a4c41413a192870b)
The new commit on main makes the same change with the same message and author, but it's a different commit, with its own ID and a different parent. The original stays on develop.
-x appends (cherry picked from commit ...) to the message, recording where it came from. Use it when copying between public branches, such as backporting a fix to a release branch, so anyone can trace the copy back. Leave it out when the original will never be shared, as the reference would point to a commit nobody else can see.
Other useful options:
| Option | Effect |
|---|---|
-e | edit the message before committing |
-n (--no-commit) | apply the changes to the working tree and staging area, without committing. Useful for combining several picks into one commit, or adjusting the change first |
-s | add a Signed-off-by: trailer |
--ff | if the current commit is the picked commit's parent, fast-forward to it instead of copying |
Picking several commits
git cherry-pick a1b2c3d e4f5a6b # these two, in this order
git cherry-pick A..B # every commit after A, up to and including B
git cherry-pick A^..B # A too
git cherry-pick main..feature # everything on feature that main doesn't have
As in git log, A..B excludes A itself, which surprises many people: add ^ to include it. Commits are applied oldest first.
Conflicts and empty picks
A pick can conflict like a merge. Git stops, you resolve, git add, and continue. The commands mirror those of rebase:
| Command | Effect |
|---|---|
git cherry-pick --continue | commit the resolved pick and carry on with the rest |
git cherry-pick --skip | drop the current commit and carry on |
git cherry-pick --abort | cancel everything and return to where you started |
git cherry-pick --quit | stop, keeping the commits already picked |
Here "ours" is your current branch (HEAD) and "theirs" is the commit being picked, which matches intuition. In a rebase, it's the other way round (article 14).
If a commit's changes are already present on the current branch, the pick produces no change. Git stops and asks what to do:
git init -q trail-guide && cd trail-guide
echo "# Trail guide" > README.md; git add .; git commit -q -m "Start project"
git switch -q -c develop
echo "Lake path: 5 km" > lake.txt; git add .; git commit -q -m "Add lake page"
sed -i 's/guide/Guide/' README.md; git commit -q -am "Fix title capitalization"
echo "Forest walk: 3 km" > forest.txt; git add .; git commit -q -m "Add forest page"
git switch -q main
git cherry-pick develop~1 > /dev/null # the title fix is already picked
echo "=== pick develop~2..develop: the title fix, then the forest page"
git cherry-pick develop~2..develop
echo "=== skip the empty one"
git cherry-pick --skip
git log --oneline
=== pick develop~2..develop: the title fix, then the forest page
On branch main
The previous cherry-pick is now empty, possibly due to conflict resolution.
If you wish to commit it anyway, use:
git commit --allow-empty
Otherwise, please use 'git cherry-pick --skip'
You are currently cherry-picking commit 670a756.
(all conflicts fixed: run "git cherry-pick --continue")
(use "git cherry-pick --skip" to skip this patch)
(use "git cherry-pick --abort" to cancel the cherry-pick operation)
nothing to commit, working tree clean
=== skip the empty one
[main 337cf84] Add forest page
Date: Mon Mar 2 10:00:00 2026 +0000
1 file changed, 1 insertion(+)
create mode 100644 forest.txt
337cf84 Add forest page
9778704 Fix title capitalization
7fb62a0 Start project
The range develop~2..develop started after "Add lake page", so the lake page wasn't picked. The title fix was already on main, so its pick was empty, and --skip dropped it and carried on. To handle this automatically, use --empty=drop to drop such commits silently, or --empty=keep to record them as empty commits.
Picking a merge commit
A merge commit has two parents, so "its changes" depend on which parent you compare with. Git refuses unless you choose with -m: git cherry-pick -m 1 <merge> applies everything the merge brought into its first parent, the branch it was merged into. That's usually what you want, but cherry-picking merges is rarely the best approach: merging the branch, or picking its individual commits, keeps history clearer.
When to cherry-pick, and when not to
Good uses:
- Backporting a fix to a release or maintenance branch: fix on
main, thengit cherry-pick -x <fix>onrelease-2.4. - Rescuing a commit from a branch you're abandoning.
- Hotfixes that must reach production before the rest of a branch.
Signs another tool fits better:
- You're picking most of a branch. Merge it, or rebase it.
- The same commit is copied to many branches repeatedly. Each copy is a separate commit, and later merges between those branches may produce conflicts or apparent duplicates. Consider fixing on the oldest branch that needs it and merging forward instead.
- You want to move commits, not copy them. Cherry-pick, then remove the originals with a rebase or reset (articles 11 and 15).
Which commits are missing? git cherry and friends
Because picked commits get new IDs, git log main..develop still lists a commit that was cherry-picked into main: the original isn't an ancestor of main. To compare by content instead, Git computes a patch ID for each commit, a hash of its diff, ignoring line numbers and whitespace, and treats commits with the same patch ID as equivalent:
git init -q trail-guide && cd trail-guide
echo "# Trail guide" > README.md; git add .; git commit -q -m "Start project"
git switch -q -c develop
echo "Lake path: 5 km" > lake.txt; git add .; git commit -q -m "Add lake page"
sed -i 's/guide/Guide/' README.md; git commit -q -am "Fix title capitalization"
echo "Forest walk: 3 km" > forest.txt; git add .; git commit -q -m "Add forest page"
git switch -q main
git cherry-pick develop~1 > /dev/null
echo "=== git log main..develop: lists all three, by ancestry"
git log --oneline main..develop
echo "=== git cherry -v main develop: + not yet in main, - already there"
git cherry -v main develop
echo "=== both sides, = marks equivalent commits"
git log --oneline --cherry-mark --left-right main...develop
echo "=== only develop's commits that main lacks"
git log --oneline --cherry-pick --right-only main...develop
=== git log main..develop: lists all three, by ancestry
9a01331 Add forest page
670a756 Fix title capitalization
d2a3ed1 Add lake page
=== git cherry -v main develop: + not yet in main, - already there
+ d2a3ed1aa445943195274bb8da514dc9531cf318 Add lake page
- 670a756286a6730bda2546b3a4c41413a192870b Fix title capitalization
+ 9a01331ae3f909799f904ebe420b38617b9ebc9b Add forest page
=== both sides, = marks equivalent commits
= 9778704 Fix title capitalization
> 9a01331 Add forest page
= 670a756 Fix title capitalization
> d2a3ed1 Add lake page
=== only develop's commits that main lacks
9a01331 Add forest page
d2a3ed1 Add lake page
git cherry -v <upstream> <branch>lists the commits on<branch>that aren't in<upstream>, marking each+if no equivalent exists in<upstream>, and-if one does. "Which of my commits still need to go upstream?" isgit cherry -v main.git log --cherry-mark --left-right A...Bshows both sides, marking equivalent commits with=, and the rest with<or>.git log --cherry-pick --right-only A...B, or its short form--cherry, lists onlyB's commits with no equivalent inA.git rev-list B ^Alists commit IDs reachable fromBbut notA, for scripts. It takes several branches, as ingit rev-list feature1 feature2 ^main.
Comparing two versions of a branch: range-diff
After rebasing or cherry-picking a series of commits, git range-diff compares the old and new versions commit by commit, showing which commits changed and how:
git range-diff main feature@{1} feature # feature before and after its last rebase
git range-diff old-base..old-tip new-base..new-tip
Each line pairs an old commit with its new version: = for unchanged, ! with a diff of the differences, and < or > for commits only on one side. Reviewers use it to see what changed between two pushes of a pull request.
What's current
--empty=drop|keep|stopcontrols what happens to picks that become empty. It replaces the older--keep-redundant-commits, now deprecated.- Hosting services can cherry-pick for you: GitHub's and GitLab's interfaces can open a backport pull request that cherry-picks a merged change onto another branch.
git replayis a newer, experimental command for replaying commits onto another base without touching the working tree, aimed at servers and scripts.
Common questions
Does cherry-picking move the commit? No. It copies its changes into a new commit. The original stays where it was.
Why does git log main..develop still show a commit I cherry-picked?
The copy on main is a different commit, so the original isn't an ancestor of main. Use git cherry -v main develop or git log --cherry-pick --right-only main...develop, which compare content.
I cherry-picked and then merged the branch anyway. Will the change appear twice? Usually not. The merge sees the same change on both sides and keeps one copy. It can conflict if the lines were changed again afterwards on one side.
Should I always use -x?
Use it when picking between shared branches, so the source can be traced. Skip it when the original commit is private and will be thrown away.
Summary
git cherry-pick <commit>copies a commit's changes onto the current branch as a new commit.-xrecords the source.A..Bpicks the commits afterAup toB. UseA^..Bto includeA.- Resolve conflicts, then
--continue, or--skip,--abortor--quit.--empty=dropdrops picks that are already present. - Merge commits need
-m 1to choose the parent. - Cherry-pick for backports and rescues. Merge or rebase when you want most of a branch.
git cherry -v,git log --cherry-markand--cherry-pickcompare branches by content, so they recognize picked commits.git range-diffcompares two versions of a series.
Practice questions
A fix was committed on main as c0ffee1. How do you bring it to the release-2.4 branch so the source can be traced?
Answer
git switch release-2.4
git cherry-pick -x c0ffee1
-x adds (cherry picked from commit c0ffee1...) to the new commit's message.
develop has commits D1, D2, D3, D4, oldest first. Which commits does git cherry-pick D1..D3 apply, and how do you include D1?
Answer
It applies D2 and D3: a range A..B excludes A. To include D1, use git cherry-pick D1^..D3.
Two of the five commits on feature were cherry-picked into main last week. Which command lists only the three that main still lacks?
Answer
git cherry -v main feature lists all five, marking the three missing ones with + and the two picked ones with -. To list only the missing three: git log --oneline --cherry-pick --right-only main...feature. git log main..feature would list all five, because it compares ancestry, not content.
Review quiz
Test yourself on Part 3 with the Git Part 3 review quiz.
Previous: Interactive Rebase: Squash, Fixup, Reword, Reorder, Edit and Autosquash
Next: Remote Repositories: add, rename, remove, set-url, show and ls-remote

More in Git Course
Quiz: Git Delicate Details
Twenty-five questions on Git's subtle corners: what commit -a and stash leave out, empty folders, ignore patterns and negation, HEAD~ versus HEAD^, ours and theirs in a rebase, tags that don't move or don't push, describe, merge reverts, diff exit codes, stash conflicts and more.
Quiz: Git Interview Questions
Thirty Git questions of the kind asked in developer interviews: Git versus GitHub, the staging area, merge versus rebase, fetch versus pull, reset, revert and restore, detached HEAD, conflicts, force pushing, stash, cherry-pick, reflog, bisect, submodules, LFS and how commits are identified.
Quiz: Git Course Final Exam (All Parts)
The final exam for the Git course: thirty questions across all twelve parts, from the three areas and staging through branching, remotes, undoing, history investigation, configuration, tags, larger projects, patches, rewriting, internals and troubleshooting.
Quiz: Professional Git Practice (Git Part 12)
Sixteen questions on migrating to Git from Subversion, TFVC and Mercurial, team best practices for commits, branches and reviews, repository hygiene, and diagnosing and fixing the most common Git errors, with explanations.
Discussion
Sign in to ask a question or share what you learned.