git cherry-pick for AI agents: move one fix between branches
TL;DR: git cherry-pick copies one commit onto the current branch as a brand new commit with a new SHA. It is the move an AI agent needs when it fixed a bug on the wrong branch: you do not redo the work, you do not rewrite the branch you already pushed, and you do not hand-edit files and hope the diff matches. Every output below was produced by Git 2.47.3 on Nov 24 2026 in throwaway repositories, not reconstructed from memory.
| The situation | The command | What actually happens |
|---|---|---|
The agent fixed the bug on feature, you need it on main | git cherry-pick feature | New commit, new SHA, same diff |
| The cherry-pick hit a conflict | git cherry-pick --abort | Restores the branch exactly as it was |
| You resolved the conflict by hand | git add <file> then git cherry-pick --continue | Creates the commit with your resolution |
| You want a traceable record of where the fix came from | git cherry-pick -x <sha> | Adds “(cherry picked from commit …)” to the message |
| You want to batch three fixes into one commit | git cherry-pick --no-commit <sha> | Stages the changes, creates no commit |
| The fix is already on this branch | git cherry-pick --skip | Drops the empty cherry-pick, keeps history clean |
| You need the same commit on three branches | git cherry-pick <sha> on each | One diff, three SHAs, three reviewable commits |
Why an agent needs this command
The classic agent failure is not “the agent wrote bad code”. It is “the agent wrote good code on the wrong branch”. The agent was told to fix issue #412 on main, but the session had the repository checked out on a feature branch, so the patch landed there. Now you have two choices: rebase the branch (rewrites history, painful if it was pushed) or cherry-pick (adds one commit, changes nothing that already exists).
Our git worktree guide exists to stop that situation from happening, and git stash handles the “park the work” case. Cherry-pick handles the “the work is already committed, on the wrong branch” case, which is the one left over after those two.
What cherry-pick actually does
A cherry-pick takes the diff of one commit and applies it to the current branch as a new commit. The commit gets a new SHA because its parent is now different. Here is a clean case, run on Git 2.47.3:
$ git cherry-pick feature
[main e370db2] fix: extend b.txt
Date: Tue Nov 24 09:23:51 2026 +0800
1 file changed, 1 insertion(+)
exit=0
$ git rev-parse feature
2ace0c27fa6d9571d3aa4b7db8a82344726fe0f1
$ git rev-parse main
e370db25cdec96a26bde12d033ba501fb3880f2a
Two different SHAs, one identical diff. The author date is preserved, which is why the commit still shows the original timestamp.
The conflict path, exactly as Git prints it
When the same lines changed on both sides, Git stops with exit code 1 and writes the conflict into the file:
$ git cherry-pick feature
Auto-merging a.txt
CONFLICT (content): Merge conflict in a.txt
error: could not apply 42d8a10... fix: correct line 2
hint: After resolving the conflicts, mark them with
hint: "git add/rm <pathspec>", then run
hint: "git cherry-pick --continue".
hint: You can instead skip this commit with "git cherry-pick --skip".
hint: To abort and get back to the state before "git cherry-pick",
hint: run "git cherry-pick --abort".
hint: Disable this message with "git config advice.mergeConflict false"
exit=128
Aborting is safe and complete
git cherry-pick --abort returns the branch to the state it was in before the attempt. This is the command to reach for when a conflict is in a file the agent cannot resolve correctly, for example a lockfile or a generated file:
$ git cherry-pick --abort
exit=0
$ git status --porcelain
$ cat a.txt
line1
line2-changed-on-main
line3
The empty git status --porcelain output is the proof that nothing was left behind. This matters for agents, because a half-applied cherry-pick left in the working tree is exactly the state that makes the next agent session make a worse decision.
Resolving by hand and continuing
Edit the file, stage it, and continue:
$ printf 'line1\nRESOLVED-BY-HAND\nline3\n' > a.txt
$ git add a.txt
$ GIT_EDITOR=true git cherry-pick --continue
[main 0405430] fix: correct line 2
1 file changed, 1 insertion(+), 1 deletion(-)
exit=0
$ cat a.txt
line1
RESOLVED-BY-HAND
line3
GIT_EDITOR=true is the standard trick for non-interactive sessions: it stops Git from opening an editor for the commit message. In an agent runtime you want that flag, or a GIT_EDITOR set to true in the environment, or the cherry-pick will hang waiting for a terminal that does not exist.
Three flags that change the outcome
-x records the origin. This is the one to use when a human will review the commit later:
$ git -c core.editor=true cherry-pick -x feature
[main 4b4aa73] fix: extend b.txt
$ git log -1 --format=%B
fix: extend b.txt
(cherry picked from commit 3ccc7fa71105822450c16df5da5bb9f7331aa20b)
--no-commit stages without committing. Useful when the agent needs to fold several fixes into one reviewed commit:
$ git cherry-pick --no-commit feature
exit=0
$ git status --porcelain
M b.txt
$ git log --oneline -1
c61d5d8 base
Note that the file is staged (M in the first column) and HEAD has not moved. Nothing is committed until you run git commit yourself.
--skip cleans up an empty result. Cherry-picking a commit that is already applied produces this message and exit code 1:
$ git cherry-pick feature
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'
nothing to commit, working tree clean
exit=1
git cherry-pick --skip returns exit 0 and leaves the branch exactly where it was. This is the difference between “the agent reported failure and stopped” and “the agent recognised that the change was already there and moved on”.
Two failure modes worth knowing
Merge commits need a parent number. A merge commit has two parents, so Git cannot guess which side you mean:
$ git -c core.editor=true cherry-pick -m 1 <merge-sha>
[target a1700ab] merge side
1 file changed, 1 insertion(+)
exit=0
-m 1 means “the diff against the first parent”. -m 2 means the diff against the second parent. Getting this wrong applies the wrong diff, so an agent that cherry-picks a merge should always pass -m explicitly.
A bad SHA fails loudly. There is no partial application:
$ git cherry-pick deadbeefdeadbeefdeadbeefdeadbeefdeadbeef
fatal: bad object deadbeefdeadbeefdeadbeefdeadbeefdeadbeef
exit=128
Exit code 128, not 1. If your agent script branches on exit codes, that difference is worth encoding: 1 means “conflict, needs a human or a resolution step”, 128 means “the object does not exist, fix the ref”.
Cherry-picking a range
A range applies several commits in order:
$ git cherry-pick main~2..main
[other e2a1cf6] c1
[other 91b3f3c] c2
exit=0
The A..B form excludes A itself, so main~2..main picks up the two commits after main~2. If you want the base commit included as well, use main~2^..main. Each commit in the range becomes its own commit, so a conflict in the third one leaves the first two applied.
Cherry-pick, rebase, stash, or worktree?
| Goal | Command | Rewrites existing commits? |
|---|---|---|
| Move one commit to another branch | git cherry-pick | No |
| Move a line of commits and flatten them | git rebase | Yes, on the branch you are on |
| Park uncommitted work | git stash | No |
| Work on two tasks at once | git worktree | No |
The rule that keeps agents out of trouble: cherry-pick adds, rebase rewrites. If the branch was already pushed and someone else has it, rebase forces a force-push. Cherry-pick does not.
If you want to compare two versions of the same change rather than move it, our git range-diff guide covers the diff-of-diffs. If the problem is “the agent edited files I did not want it to touch”, that is a scope problem, and the path-scope check is the guard. If the branch was deleted entirely, git reflog may still have the commit.
Honest take
git cherry-pick is a small command with a small surface area, and that is exactly why it is useful to an agent. It does one thing, it refuses to guess, and it leaves a file behind that tells you exactly what it was doing when it stopped.
The failure mode is not a wrong result, it is a stalled result: a conflict that the agent cannot resolve, leaving CHERRY_PICK_HEAD on disk and every subsequent Git command refusing to run. If your agent runtime is non-interactive, make sure it has a policy for exit code 1 versus exit code 128, and make sure git cherry-pick --abort is always available as the escape hatch.
One more practical note: a cherry-picked commit is a different SHA with the same diff. If your review tooling compares SHAs, it will see a new commit. If it compares diffs, it will see nothing new. Both behaviours are correct, and confusing them is a common source of “the agent says it pushed but nothing changed” reports.
Sources
- git-cherry-pick - Git reference
- git-rebase - Git reference
- git-stash - Git reference
- git-worktree - Git reference
- git-reset - Git reference
- Git 2.47.3 release notes
Last updated Nov 24 2026. Command output in this article was reproduced with Git 2.47.3 on Nov 24 2026. Behaviour can change between Git versions; check git --version and the installed documentation before relying on exact message wording.
Was this article helpful?
Thanks for the feedback — it helps improve future articles.
Need hands-on help?
I offer 1-on-1 technical consulting for local AI setup, GPU selection, and AI coding tool configuration — same topics covered on this site.
Book a session — $49 / hour →Know which coding tool is worth paying for
Hands-on comparisons of AI coding assistants and what each one costs to run — including the local-model path. Sent only when something changes. Unsubscribe anytime.