← Back to BlogAgentic Coding

Claude Code Pushed to Main? How to Stop It

Claude Code committed, pushed or merged before you reviewed? Why auto mode allows it, plus the ask rule, hooks and branch protection that limit it.

Claude Code runs git commit, git push and sometimes gh pr merge without asking whenever its permission mode or a saved allow rule lets it, and your own instructions can make it try. The fix that holds is layered: an ask rule and a hook inside Claude Code, a pre-push hook in git, and branch protection on GitHub, because each layer misses something another one catches.

Check the permission mode first. The docs say auto mode is the built-in starting mode for interactive terminal and VS Code sessions with Claude Code v2.1.283 or later, and the classifier behind it allows pushing to any branch of your repo by default, the default branch included. If you never chose that mode, an unexpected push may be the documented default.

Sources: Claude Code docs: Which mode a session starts in, Claude Code docs: What the classifier blocks by default

Disclosure: we build TLM Forge, a process layer for Claude Code, so weigh the product section accordingly. Everything before that section applies with or without us.

Why Claude Code pushed without asking

In Manual mode, Bash commands need your approval except a built-in set of read-only commands, so a git commit or git push prompts. A push that skips the prompt usually came from one of these routes.

Sources: Claude Code docs: Configure permissions

  • Auto mode. A classifier reviews actions in your place. The auto mode docs say: "By default in auto mode, Claude can push to the repository you're working in, including its default branch, without a prompt."
  • Bypass mode. --dangerously-skip-permissions and bypassPermissions skip permission prompts. Allow rules have no effect there. Deny rules and explicit ask rules still apply, and so does a hook that denies.
  • Saved approvals. Choosing "Yes, and don't ask again" for a Bash command saves a rule permanently for that repository in .claude/settings.local.json. A broad rule such as Bash(git *) allows every git subcommand, push included.
  • Background sessions. In a worktree Claude entered, a background session is told to commit without asking and, when the repository has a remote, push the branch. It is also told never to push to main or master, force-push or merge, and to leave git to you when the task, CLAUDE.md or memory says you handle committing or pushing.
  • Your own instructions. A CLAUDE.md line such as "commit when the tests pass", or a skill that ends in git push, tells Claude to attempt it. Where the mode or your rules do not prompt, no prompt stands in the way.

You can read the shipped auto mode rules yourself. claude auto-mode defaults prints them as JSON. On v2.1.284 the allow list has a rule named Git Push Destination, which treats pushing to any branch of the session's repo as ordinary: "the default branch included, whatever its name; no branch etiquette is enforced". One carve-out: a non-default branch whose name marks it as a deploy or publication target, such as production or gh-pages, is judged on its own terms. The same output lists force pushes, amending a commit that is not the agent's own unpushed work, and merging a pull request before any human has approved it among the actions blocked by default. It also says gh pr merge --auto on a repo with required-reviews branch protection is not a merge without review, because "the gate is server-enforced".

Sources: Claude Code docs: Run claude auto-mode defaults, Claude Code docs: Permission modes

None of this is hidden. The docs say "Auto mode reduces permission prompts but does not guarantee safety." They also say the remote's own branch protection still applies, and that a boundary you state in chat, such as "don't push", can be lost if context compaction removes the message that stated it. Treat the classifier as one layer among several.

Sources: Claude Code docs: Permission modes, Claude Code docs: What the classifier blocks by default, Claude Code docs: Boundaries you state in conversation

Two GitHub issues describe the same surprise. In issue 95749, opened September 20, 2026 against v2.1.278, one reporter says that with defaultMode set to auto, no allow, ask or deny rules, an unprotected main and a broadly scoped GitHub credential, Claude pushed a 14-file commit straight to origin/main that they had not asked for. In issue 20401, from January 2026, another reporter says Claude committed without approval three times across sessions despite a CLAUDE.md rule and a large warning banner. Both are single reports, not measurements, and neither says how often this happens.

Sources: anthropics/claude-code issue 95749, anthropics/claude-code issue 20401

What each permission mode does with git

This table covers four of the permission modes. acceptEdits prompts for commit, push and merge commands the way Manual mode does. In plan mode, when auto mode is available and useAutoModeDuringPlan is on, which it is by default, the classifier reviews shell commands instead of prompting you, so a prompt is not guaranteed there.

Sources: Claude Code docs: Permission modes, Claude Code docs: dontAsk mode, Claude Code docs: acceptEdits mode, Claude Code docs: Plan mode

ModeCommit, push and merge commandsWhat still stops them
Manual (default)Each one prompts, unless an allow rule matches, including a rule saved from "don't ask again"Your answer at the prompt
autoReviewed by the classifier, unless an allow, ask or deny rule matches first. Pushing to any branch of the repo, default branch included, is allowed by default. Force pushes and merging a pull request no human approved are blocked by defaultAsk and deny rules, a hook that asks or denies, a stated boundary until compaction drops it, and the remote branch protection
dontAskDenied without a prompt unless an allow rule or a hook approves itYour allow list
bypassPermissionsEverything runs without a promptDeny rules, explicit ask rules and a hook that denies

Fix 1: choose the starting mode, then audit your allow rules

To get a prompt for every commit, push and merge, start sessions in Manual mode. A permissions.defaultMode in your user settings overrides the built-in default. To take auto mode away entirely, disableAutoMode set to "disable" makes any session that would start in auto mode start in default instead and removes auto from the Shift+Tab cycle.

Sources: Claude Code docs: Which mode a session starts in, Claude Code docs: Settings reference

  • Manual by default, in ~/.claude/settings.json: { "permissions": { "defaultMode": "default" } }
  • No auto mode at all, in ~/.claude/settings.json: { "disableAutoMode": "disable" }

A starting mode is not a lock, because you can change a running session's mode at any time. To lock out bypass mode, set permissions.disableBypassPermissionsMode to "disable", which makes Claude Code reject --dangerously-skip-permissions.

Sources: Claude Code docs: Permission modes, Claude Code docs: Settings reference

Then audit what you already allowed. /permissions lists every rule with the settings file it comes from. Look for Bash(git push *), Bash(git *) and Bash(gh pr merge *), and open .claude/settings.local.json at the repo root, where "don't ask again" rules land. Replace broad rules with narrow ones. The docs' own example allows Bash(git commit *) and denies Bash(git push *).

Sources: Claude Code docs: Configure permissions, Claude Code docs: Wildcard patterns

Fix 2: an ask rule, then a hook for the spellings it misses

If you want to stay in auto mode, an ask rule is the documented checkpoint. Ask rules are evaluated before the classifier, so by default a matching command prompts. A deny rule "Blocks before the classifier is consulted. Neither the classifier nor user intent can override it." Prefer ask for push and merge, because you will sometimes want to say yes. In an unattended -p run nobody can answer: from v2.1.302 such a run starts in Manual mode when no mode is set, and the docs say a call that needs approval is denied unless a rule, hook or callback approves it.

Sources: Claude Code docs: Configure auto mode, common boundaries, Claude Code docs: Permission modes

The limit is spelling. Bash(git push *) matches commands that begin with git push. The docs list git -C . push origin main and git -c push.default=current push origin main as commands it does not match, and say a deny or ask rule "isn't a security boundary around the program." The auto mode docs give the next step: for a checkpoint that inspects the full command text, add a PreToolUse hook.

Sources: Claude Code docs: What a Bash rule doesn't match, Claude Code docs: Configure auto mode, common boundaries

This settings block combines both. Commit it as .claude/settings.json in the repo, next to the script below. The onFailure field needs v2.1.295 or later, so leave it out on older versions.

Sources: Claude Code docs: Block the action when a hook fails

  • { "permissions": { "ask": ["Bash(git push *)", "Bash(gh pr merge *)"] }, "hooks": { "PreToolUse": [{ "matcher": "Bash", "hooks": [{ "type": "command", "command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/git-gate.sh", "onFailure": "block" }] }] } }

The script is five lines. Save it as .claude/hooks/git-gate.sh, run chmod +x on it, and keep the lines in this order:

  • #!/bin/bash
  • command -v jq >/dev/null || { echo "git-gate: jq is required" >&2; exit 2; }
  • cmd=$(jq -r '.tool_input.command // ""') || exit 2
  • grep -Eq '\bgit\b.*\b(push|merge)\b|\bgh\b.*\bmerge\b' <<<"$cmd" && jq -n '{hookSpecificOutput: {hookEventName: "PreToolUse", permissionDecision: "ask", permissionDecisionReason: "This command may publish or merge work. Confirm it."}}'
  • exit 0

The hook prints a permissionDecision of ask, which shows the normal permission prompt. In auto mode that ask normally goes to the prompt and the classifier does not decide it. The script fails closed: if jq is missing, the second line says so and exits 2, which blocks the command. onFailure set to "block" covers a script that crashes, times out or is missing. Without it, on most setups those are non-blocking errors and the command goes ahead. Add commit inside the (push|merge) group if you also want to approve every commit.

Sources: Claude Code docs: Hooks reference, Claude Code docs: PreToolUse ask decisions in auto mode, Claude Code docs: Block the action when a hook fails

Tested against sample commands with git 2.54.0 and jq 1.7.1. It asked for git push, git -C . push, git -c push.default=current push, git 'push', a chained git add && git commit && git push, bash -c 'git push' and gh pr merge --auto. It stayed silent for git status, git commit and npm test, and also for sh deploy.sh, an alias such as git ship, and a command built from a variable, P=push; git $P. That silence is the limit: the hook reads the text Claude writes, not what a script or alias does. The pattern also asks when a commit message or search term contains the word push, which costs one extra click.

Pro Tip

To confirm the hook is live, ask Claude to run git -C . push --dry-run. You should get a permission prompt labeled [settings], the label the docs describe for a hook from a settings file. Answer No.

For unattended runs, dontAsk mode flips the default. It denies every call that would prompt, so apart from read-only commands, a command runs when it matches an allow rule or a hook approves it. A push written in a way no allow rule matches is denied, where a deny rule would have missed it. One Hacker News commenter describes running dontAsk with some git commands allowed, "git commit yes, git push no".

Sources: Claude Code docs: dontAsk mode, Hacker News comment by sheept

Fix 3: refuse pushes at the git level

A pre-push hook sits below Claude Code. Git calls it on every push that does not skip hooks, however the command is spelled, and a non-zero exit stops the push. Claude Code sets CLAUDE_CODE_CHILD_SESSION to 1 in subprocesses it spawns, including those from its Bash and PowerShell tools. Unlike CLAUDECODE, IDE extensions do not set it, so a push from your own terminal passes. The docs list it as requiring v2.1.172 or later.

Sources: git-scm.com: githooks, Claude Code docs: Environment variables

Save these three lines as .git/hooks/pre-push and run chmod +x on the file:

  • #!/bin/sh
  • [ -n "$CLAUDE_CODE_CHILD_SESSION" ] && { echo "pre-push: blocked. Push from your own terminal." >&2; exit 1; }
  • exit 0

Tested with git 2.54.0 against a local bare remote, with the variable set. A plain git push, git -C . push, git -c push.default=current push, git 'push', an alias for push and a shell script that runs git push were all refused. Three routes got through: git push --no-verify, which the git docs say bypasses the hook completely; git -c core.hooksPath=/dev/null push; and env -u CLAUDE_CODE_CHILD_SESSION git push. The hook stops routine pushes. It does not stop an agent that deliberately overrides it.

Sources: git-scm.com: git-push, git-scm.com: githooks

Keep the file in .git/hooks, not in a tracked folder such as .githooks. Claude Code lists .git and .claude as protected paths: a write there prompts in Manual mode, goes to the classifier in auto mode and is allowed in bypassPermissions mode. A .githooks folder is not on the list. If you use Husky or lefthook, add the same check to that tool's pre-push step; .husky and lefthook.yml are protected too.

Sources: Claude Code docs: Protected paths

A git hook cannot see a merge. gh pr merge merges a pull request on GitHub, so no local push happens. The environment variable is also not set for stdio MCP server subprocesses, so a git tool run through an MCP server is outside this check.

Sources: GitHub CLI manual: gh pr merge, Claude Code docs: Environment variables

Fix 4: make GitHub refuse the merge

Everything above runs on your machine, where it can be edited, bypassed or spelled around. The one layer that does not care which client sent the push is the server. A branch protection rule disables force pushes to matching branches by default. With required reviews on, collaborators can only push to a protected branch through a pull request approved by the required number of reviewers. gh pr merge --auto merges only after necessary requirements are met, so the rules on the branch decide when it fires.

Sources: GitHub Docs: About protected branches, GitHub CLI manual: gh pr merge

Two defaults matter for agents. First, a rule's restrictions "don't apply to people with admin permissions" unless you turn on "Do not allow bypassing the above settings". The Claude Code security docs say a Bash command you approve can still write anywhere your user account can. Claude's git and gh commands run as that account too, so if the credentials they use belong to a repository admin, a push or merge Claude runs can use that bypass, and gh pr merge --admin is documented as using administrator privileges to merge a pull request that does not meet requirements. Second, GitHub says "Pull request authors cannot approve their own pull requests", so a solo maintainer cannot satisfy a required review on a pull request their own account opened. A real gate needs a second identity: another collaborator, or a separate account for the agent.

Sources: GitHub Docs: About protected branches, GitHub CLI manual: gh pr merge, GitHub Docs: Approving a pull request with required reviews, Claude Code docs: Security

Per GitHub's docs, protected branches work on private repositories only with a paid plan (Pro, Team or Enterprise). To limit what the agent's credentials can reach in the first place, see Least-Privilege Access for AI Coding Agents. To decide which changes need a human sign-off at all, see Approval Gates for High-Risk AI Code Changes.

Sources: GitHub Docs: About protected branches

The layers compared

Each layer answers a different failure, and each has a blind spot the next one covers. The deny and ask mechanics are covered in more depth in Claude Code Permissions: What Deny Rules Miss, and the classifier's behavior in Claude Code Auto Mode: From Too Loose to Too Strict.

LayerWhat it stopsWhat slips pastWhat can undo it
CLAUDE.md, or "don't push" in chatNothing mechanically. In auto mode the classifier reads both, so they steer itAny lapse by the model or classifier. A chat boundary is lost if compaction drops the messageCompaction, a later instruction, a misreading
ask rule on Bash(git push *)A prompt for commands that begin with git push, in auto and bypass modes toogit -C . push, git -c ... push, scripts, aliasesYou, through /permissions or by editing settings
PreToolUse hook scanning the commandMost spellings of push and merge in a Bash command, including chained and wrapped onesScripts, aliases, commands built from variables, tools other than BashWhoever edits the hook or settings. Writes to .claude are allowed in bypass mode
Git pre-push hook on CLAUDE_CODE_CHILD_SESSIONA git push from a shell Claude Code starts, including git -C . push, aliases and scripts that run git push--no-verify, a changed hooks path, an unset variable, gh pr merge, stdio MCP serversAny command that uses one of those overrides
GitHub branch protection ruleDirect pushes to the protected branch and merges without required reviews or checks, from any clientAdmins and bypass roles unless bypassing is off. A solo account cannot approve its own pull requestRepository admins

A checklist to copy

  • Run /permissions and delete any Bash(git *), Bash(git push *) or Bash(gh pr merge *) allow rule you did not mean to keep.
  • Set permissions.defaultMode to "default", or disableAutoMode to "disable", in ~/.claude/settings.json.
  • Add the two ask rules and the git-gate.sh hook.
  • Install the pre-push hook in .git/hooks if Claude should never push for you.
  • Protect main: require a pull request and approvals, and turn on "Do not allow bypassing the above settings".
  • Run claude auto-mode defaults after each upgrade and reread the push and merge rules.

Where TLM Forge fits, and where it does not

TLM Forge adds one control aimed at this problem. A commit hook blocks git commit, git push and gh pr merge when the code changed since the final review. It is on for everyone, and TLMFORGE_HOOKS=0 turns hooks off.

The limits are the ones this post describes. The hook matches the command text of those three commands, so a push by another route is outside what it sees, and it does not replace an ask rule, the git hook or branch protection. It answers whether reviewed code has changed since, not whether Claude may push at all.

Frequently asked questions

01Can Claude Code push to main without asking?

Yes, in some setups. In auto mode the default rules allow pushing to any branch of the repo you are working in, the default branch included, without a prompt. Manual mode asks first unless an allow rule matches. Branch protection on the remote still applies in either mode.

02Does Claude Code commit automatically?

Not in Manual mode, which prompts for git commit unless an allow rule matches. Auto mode, bypass mode, saved allow rules and background sessions can commit without a prompt. To approve every commit, add an ask rule for Bash(git commit *) or add commit to the hook pattern.

03How do I make Claude Code ask before git push?

Add an ask rule for Bash(git push *) in settings.json. It prompts even in auto mode. Rules match command text, so git -C . push is not matched. A PreToolUse hook that returns an ask decision for the full command text covers more spellings.

04Does a CLAUDE.md rule stop Claude Code from committing?

Not mechanically. The docs say Claude treats CLAUDE.md as context, not enforced configuration. In auto mode the classifier also reads it, so it steers decisions, but one GitHub issue reports commits despite an explicit rule. Use a deny or ask rule, or a hook, for a boundary that must hold.

05Can Claude Code merge a pull request on its own?

It can run gh pr merge if your mode or rules allow it. Auto mode blocks merging a pull request no human has approved by default, and background sessions are told never to merge. GitHub required reviews and branch protection apply whichever client runs the command, unless the account can bypass them.

Ship AI-written code you can trust

TLM Forge is the missing process layer for Claude Code: a spec audit, independent multi-agent review, enforced TDD, and an adversarial red-team gate.

Get TLM Forge