PricingSign inDownload

Blog / git

Take one fix from a coding agent's branch

An agent's branch has one commit you need now and others that aren't ready. Cherry-pick it, a range, or a single file, and handle a conflict.

On this page
  1. Find the commit
  2. Copy it to main
  3. Several commits
  4. Only one file
  5. It stopped on a conflict
  6. The commit is a merge
  7. Check it still works
  8. Undo a cherry-pick
  9. When the branch is merged later
  10. Ask agents for small commits
  11. Where Joxo fits
  12. Frequently asked questions
  13. How do I copy one commit from another branch in git?
  14. How do I cherry-pick several commits?
  15. How do I take one file from another branch?
  16. What happens when I merge a branch I cherry-picked from?
  17. How do I cherry-pick a merge commit?
  18. How do I undo a cherry-pick?
  19. Why does the code fail after a clean cherry-pick?
  20. Try it with your team

An agent working on a feature fixed a bug along the way. You need the fix on main today; the feature isn't ready. You can copy just that commit across with git cherry-pick, and leave the rest of the branch where it is. We ran every command with git 2.43 on a scratch repository.

Find the commit#

List what the branch has that main doesn't:

bash
git log --oneline main..agent/search
1872e74 Search by tag
d924d33 Fix date parsing
8f39088 Add search

Check that the commit touches only what you expect with git show --stat d924d33. A commit that mixes the fix with feature work is worth splitting first.

Copy it to main#

bash
git switch main
git cherry-pick -x d924d33
[main 59d8b9f] Fix date parsing
 1 file changed, 1 insertion(+), 1 deletion(-)

The new commit has the same change and message, and a new hash. -x adds a line to the message saying where it came from:

Fix date parsing

(cherry picked from commit d924d337fe9fdbd3b96b9f8f2ba231aec3623387)

That line helps anyone who later wonders why the same fix appears twice.

Several commits#

Give a range: everything after the first hash up to and including the second. Add ^ to the first to include it as well:

bash
git cherry-pick 8f39088^..d924d33
[main 94c3fab] Add search
 ...
[main 8970301] Fix date parsing
 ...

Commits are applied oldest first. To apply the changes without committing, for example to combine them into one commit, use git cherry-pick --no-commit.

Only one file#

If you want one file from the branch as it is now, not a commit:

bash
git restore --source agent/search -- search.js

The file is copied into your working tree, uncommitted, for you to check and commit.

It stopped on a conflict#

If main has changed the same lines, the cherry-pick stops:

CONFLICT (content): Merge conflict in date.js
error: could not apply d924d33... Fix date parsing

Fix the file, then git add date.js and git cherry-pick --continue. To give up and go back to where you were:

bash
git cherry-pick --abort

The commit is a merge#

If the fix came in through a merge on the agent's branch, git asks which side to copy:

error: commit 91641f5ecc748a8480d6fb552f5974dc9143197c is a merge but no -m option was given.
fatal: cherry-pick failed

-m 1 takes the changes the merge brought into the branch it was made on:

bash
git cherry-pick -m 1 91641f5

Picking the commits on the merged branch itself is often cleaner, since they keep their own messages.

Check it still works#

A fix can depend on something an earlier commit on the branch added: a helper function, a new setting, a renamed file. The cherry-pick may apply cleanly and still not build. Run your tests on main before pushing. If it fails because something is missing, pick the earlier commit too, or ask the agent to rewrite the fix so it stands alone.

Undo a cherry-pick#

Not pushed yet: drop the new commit.

bash
git reset --hard HEAD~1

This also throws away uncommitted edits, so commit or save them first. Already pushed: add a commit that undoes it with git revert <new hash>.

When the branch is merged later#

The agent's branch still has the original commit. When it's merged, git sees that the change is already on main and usually merges it without a conflict. To check which of the branch's commits are already on main:

bash
git cherry -v main agent/search
+ 8f390881a9282f3233dd236ce70e59f0ef0f6451 Add search
- d924d337fe9fdbd3b96b9f8f2ba231aec3623387 Fix date parsing
+ 1872e741ad7f0e5268cc3072b4529fdc15af3e2e Search by tag

- means the same change is already on main; + means it isn't.

Ask agents for small commits#

Cherry-picking only works well if a fix is its own commit. Add to your project instructions: "Commit each separate fix on its own, with a message that says what it fixes, before going on with the feature."

Where Joxo fits#

With several people's agents on one project, someone often needs a fix that's sitting on another person's branch. In Joxo the agents share one task board and can message each other, so yours can ask the other agent which commit has the fix before taking it. Setup is one line pasted into your agent.

Frequently asked questions#

How do I copy one commit from another branch in git?#

Switch to the branch you want it on and run git cherry-pick <hash>. Add -x to record the original commit in the message.

How do I cherry-pick several commits?#

Use a range: git cherry-pick <first>^..<last> applies every commit from the first to the last, oldest first.

How do I take one file from another branch?#

Run git restore --source <branch> -- <file>. The file is copied into your working tree for you to commit.

What happens when I merge a branch I cherry-picked from?#

Git notices the change is already there and usually merges cleanly. git cherry -v <target> <branch> shows which commits are already applied.

How do I cherry-pick a merge commit?#

Add -m 1: git cherry-pick -m 1 <merge hash> copies the changes the merge brought into its first parent, the branch it was made on. Without -m, git refuses and says the commit is a merge.

How do I undo a cherry-pick?#

If it isn't pushed, git reset --hard HEAD~1 drops the new commit, along with any uncommitted edits. If it is pushed, git revert <new hash> adds a commit that undoes it without rewriting history.

Why does the code fail after a clean cherry-pick?#

The fix may rely on something an earlier commit on the branch added, such as a helper or a setting. Run the tests after picking, and pick the earlier commit too or have the agent make the fix stand alone.