claude-code - 💡(How to fix) Fix Claude repeatedly commits and pushes directly to main despite explicit instructions [2 comments, 3 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
anthropics/claude-code#48474Fetched 2026-04-16 06:59:08
View on GitHub
Comments
2
Participants
3
Timeline
5
Reactions
0
Author
Timeline (top)
labeled ×3commented ×2

Claude Code commits and pushes directly to main despite:

  • An explicit CLAUDE.md rule: "Never commit directly to main. All changes must go through a feature branch and a pull request."
  • A persistent memory entry (feedback_never_commit_to_main.md) created after a previous identical incident (DEV-522)
  • The memory entry being updated and re-read within the same session

Root Cause

Claude does not reliably run git branch --show-current before committing, even when the project's CLAUDE.md and persistent memory files explicitly require it. The instruction is followed inconsistently — present in memory but not enforced as a hard gate before every git commit.

RAW_BUFFERClick to expand / collapse

Description

Claude Code commits and pushes directly to main despite:

  • An explicit CLAUDE.md rule: "Never commit directly to main. All changes must go through a feature branch and a pull request."
  • A persistent memory entry (feedback_never_commit_to_main.md) created after a previous identical incident (DEV-522)
  • The memory entry being updated and re-read within the same session

What happened (DEV-551 session, 2026-04-15)

  1. After a PR was merged, Claude found itself on main but did not check the branch before committing an infrastructure fix. The commit was pushed to main, triggering a full CI build and deployment.
  2. Immediately after, Claude committed and pushed a plan documentation file directly to main — a pure docs change with zero justification for bypassing a PR. This triggered another full CI build and deployment pipeline.

Both incidents happened in the same session, after the memory entry from a prior identical incident (DEV-522) was already loaded into context.

Root cause

Claude does not reliably run git branch --show-current before committing, even when the project's CLAUDE.md and persistent memory files explicitly require it. The instruction is followed inconsistently — present in memory but not enforced as a hard gate before every git commit.

Impact

  • Triggered multiple unintended CI builds and deployments on the production branch
  • Bypassed branch protection and required code review
  • Wasted engineering time

Expected behaviour

Claude should run git branch --show-current before every git commit call. If the result is main (or master), it must stop, create a feature branch, and only then commit. This should be a hard gate, not a guideline.

extent analysis

TL;DR

Claude should be modified to always run git branch --show-current before committing and enforce a hard gate to prevent direct commits to the main branch.

Guidance

  • Verify that Claude's current implementation of git commit does not include a check for the current branch before committing.
  • Modify Claude to run git branch --show-current before every git commit call to ensure it's not on the main branch.
  • Implement a hard gate that stops the commit process and creates a feature branch if the current branch is main.
  • Review Claude's memory entry handling to ensure that loaded instructions, such as those from feedback_never_commit_to_main.md, are consistently enforced.

Example

# Pseudocode example of the pre-commit check
if [ "$(git branch --show-current)" == "main" ]; then
  # Create a new feature branch and switch to it
  git checkout -b new-feature-branch
  # Proceed with the commit
  git commit -m "Commit message"
else
  # Proceed with the commit on the current branch
  git commit -m "Commit message"
fi

Notes

The provided solution assumes that Claude's implementation allows for pre-commit checks and branch switching. The exact implementation details may vary depending on Claude's architecture and the underlying Git version.

Recommendation

Apply a workaround by modifying Claude's commit process to include a pre-commit check for the current branch, as this will prevent direct commits to the main branch and ensure that all changes go through a feature branch and a pull request.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING