PricingSign inDownload

Blog / git

Move a coding agent's commits off main to a new branch

An agent committed straight to main and you haven't pushed. Move the commits to a new branch, put main back, or carry them to an existing branch.

On this page
  1. See what's on main that shouldn't be
  2. Move them to a new branch
  3. If it also left uncommitted edits
  4. Move them to a branch that already exists
  5. Already pushed to main
  6. If you got it wrong
  7. Check main is clean again
  8. When several agents share one clone
  9. Keep it off main next time
  10. Where Joxo fits
  11. Frequently asked questions
  12. How do I move my last commits from main to a new branch?
  13. How do I move a commit to a different existing branch?
  14. Will git reset --hard delete my commits?
  15. What if the commits are already pushed to main?
  16. Try it with your team

You asked for a feature branch and the agent committed on main. If nothing has been pushed, this takes three commands and loses nothing. We ran every command with git 2.43 on a scratch repository.

First, stop the agent or let it finish, so it doesn't commit again halfway through.

See what's on main that shouldn't be#

bash
git status -sb
git log --oneline origin/main..main
## main...origin/main [ahead 2]
a2f94c8 Test the login form
92b867f Add login form

ahead 2 means two local commits that aren't on the remote yet. Those are the ones to move. Run git fetch first if you aren't sure your copy of origin/main is current.

Move them to a new branch#

Check that git status --short prints nothing: the reset below throws away uncommitted edits. Commit them, or use the next section.

bash
git branch login-form
git reset --hard origin/main
git switch login-form
HEAD is now at b6a0c7b Initial commit
Switched to branch 'login-form'

git branch makes a new branch pointing at the agent's last commit, git reset --hard moves main back to where the remote has it, and git switch puts you on the new branch. Check it:

bash
git log --oneline main..login-form
a2f94c8 Test the login form
92b867f Add login form

Both commits are on login-form, and git status -sb on main now shows ## main...origin/main with nothing ahead.

If it also left uncommitted edits#

If the agent hadn't committed yet, or left work in progress on top, take the edits with you:

bash
git switch -c login-form
Switched to a new branch 'login-form'
 M app.js

The new branch starts from the current commit and the edits come along unchanged. To move commits too, commit the edits on the new branch, then go back and reset main: git switch main && git reset --hard origin/main.

Move them to a branch that already exists#

When the work belongs on a branch you already have, copy the commits there and then reset main:

bash
git switch feature
git cherry-pick origin/main..main
git switch main
git reset --hard origin/main
Switched to branch 'feature'
[feature 1f2fa43] Add y
 1 file changed, 1 insertion(+)
 create mode 100644 y.js

origin/main..main picks every commit that's on your local main but not on the remote, oldest first. For a single commit, git cherry-pick main is enough.

Already pushed to main#

Don't reset a shared main: anyone who pulled it would have the commits back on their next push. Undo the change with a new commit instead:

bash
git revert --no-edit HEAD
[main c0488b9] Revert "Agent change"
 1 file changed, 1 deletion(-)

Push the revert, then put the change on a branch for review with git switch -c redo origin/main && git cherry-pick <hash>, using the hash of the agent's original commit. For several commits, git revert --no-edit origin/main~3..origin/main reverts the last three in one go.

If you got it wrong#

git reset --hard moved main, but the commits are still in git. git reflog lists where main has been, and git reset --hard main@{1} puts it back where it was before your last reset.

Check main is clean again#

Two checks tell you the move worked. On main:

bash
git status -sb
git diff origin/main main
## main...origin/main

The status line has nothing ahead or behind, and the diff prints nothing, so your local main is identical to the remote's. On the new branch, git log --oneline main..login-form lists the agent's commits, as shown above. If both are true, nothing was lost and nothing extra is on main.

Then push the branch and open a pull request as usual:

bash
git push -u origin login-form

When several agents share one clone#

If two agents work in the same folder, the second may commit on top of the first agent's commits before you notice. Moving commits then splits two people's work apart. List them by author first with git log --format="%h %an %s" origin/main..main, and move each agent's commits to its own branch with git cherry-pick, one branch at a time, before you reset main. Giving each agent its own git worktree avoids the problem: each worktree has its own checked-out branch, so one agent's commits can't land on another's.

Keep it off main next time#

Tell the agent which branch to use in your project instructions: "Never commit to main. Start each task with git switch -c <short-name> from origin/main." To make it a hard rule rather than a request, add a pre-commit hook that refuses commits on main (see stop a coding agent committing to main).

Where Joxo fits#

When several people's agents share one repository, a commit on the wrong branch can land in someone else's pull. Joxo gives each agent its own task on a shared board, and the task names the branch, so every agent starts in the right place. Setup is one line pasted into your agent.

Frequently asked questions#

How do I move my last commits from main to a new branch?#

Run git branch new-branch, then git reset --hard origin/main, then git switch new-branch. The commits move to the new branch and main matches the remote again.

How do I move a commit to a different existing branch?#

Switch to that branch and run git cherry-pick with the commit's hash, then switch back to main and run git reset --hard origin/main.

Will git reset --hard delete my commits?#

No. The commits stay on the branch you made first, and git reflog lists them even if you skipped that step. It does delete uncommitted edits, so check git status first.

What if the commits are already pushed to main?#

Don't rewrite a shared main. Run git revert on the commits, push that, and put the change on a branch with git cherry-pick for review.