2026-02-10 · Authensor

AI Agent Committed to Wrong Branch: How to Recover

When an AI agent commits code to the wrong git branch — especially main or production — you need to undo the damage quickly and prevent it from happening again. SafeClaw by Authensor gates git operations at the action level, so agents can only commit to branches you have explicitly permitted in your policy. If the wrong commit has already happened, follow the recovery steps below, then lock down your git policy to prevent a recurrence.

Immediate Recovery: Undo the Wrong Commit

If the Commit Has NOT Been Pushed

The fix is simple — move the commit to the correct branch:

# Save the commit hash
git log --oneline -1

Undo the commit but keep changes staged

git reset --soft HEAD~1

Switch to the correct branch

git checkout correct-branch

Commit there instead

git commit -m "your commit message"

If the Commit Has Been Pushed

You need to revert on the wrong branch and apply to the correct one:

# On the wrong branch (e.g., main)
git revert HEAD
git push origin main

Create a patch from the original commit

git format-patch -1 HEAD~1

Switch to the correct branch and apply

git checkout correct-branch git am 0001-*.patch git push origin correct-branch

If the Agent Force-Pushed to a Protected Branch

This is the worst case. Check if your remote has branch protection rules that prevented it. If not:

# Find the pre-push commit from reflog
git reflog show origin/main

Reset the remote branch to the correct state

git push origin <correct-commit-hash>:main --force

Contact your team immediately — anyone who pulled the bad commit needs to know.

Review SafeClaw's Audit Log

If SafeClaw was installed, check what git operations the agent performed:

npx @authensor/safeclaw audit --filter "action:git" --last 20

The hash-chained audit trail shows exactly which branch the agent targeted, what command it ran, and when.

Install SafeClaw and Protect Your Branches

npx @authensor/safeclaw

Configure Git Branch Protection in Your Policy

Add git operation rules to safeclaw.policy.yaml:

rules:
  # Block all git push to main/production
  - action: git.push
    resource: "main"
    effect: deny
    reason: "Direct push to main is forbidden"

- action: git.push
resource: "production"
effect: deny
reason: "Direct push to production is forbidden"

# Allow push only to feature branches
- action: git.push
resource: "feature/**"
effect: allow
reason: "Agent can push to feature branches"

- action: git.push
resource: "fix/**"
effect: allow
reason: "Agent can push to fix branches"

# Block force push entirely
- action: git.force_push
resource: "*"
effect: deny
reason: "Force push is never allowed for agents"

# Block commits to protected branches
- action: git.commit
resource: "main"
effect: deny
reason: "Agent cannot commit directly to main"

Require Branch Naming Conventions

Force agents to work on properly named branches:

rules:
  - action: git.checkout
    resource: "agent/**"
    effect: allow
    reason: "Agent works on agent-prefixed branches"

- action: git.checkout
resource: "main"
effect: deny
reason: "Agent cannot checkout main"

Troubleshooting Scenarios

Agent committed to main instead of creating a feature branch: The agent was not instructed to create a branch first. Add a policy rule that denies commits to main and the agent will be forced to work on a different branch.

Agent pushed secrets along with the wrong branch commit: This is a compound incident. First follow the branch recovery steps above, then immediately rotate any exposed secrets. See the related guide on recovering from pushed secrets.

Agent created a merge commit on the wrong branch: Revert the merge:

git revert -m 1 HEAD
git push origin wrong-branch

Then cherry-pick or rebase the changes onto the correct branch.

Prevention

SafeClaw's deny-by-default model means git operations are blocked unless explicitly allowed. Combined with 446 tests validating git action gating, you can be confident that agents will stay on their designated branches. Always:

  1. Define allowed branch patterns in your policy.
  2. Block push and force-push to main, master, and production.
  3. Use simulation mode before running new agent tasks.
  4. Review the audit trail after every agent session.

Related Resources

Try SafeClaw

Action-level gating for AI agents. Set it up in your browser in 60 seconds.

$ npx @authensor/safeclaw