Main branch detection ignores remote HEAD, hardcodes 'main'

Status Open
Reported on v2.1.37
Maintainer reply None cached
Activity 6 comments · opened Feb 9, 2026

Bug

Claude Code's system prompt injects:

Main branch (you will usually use this for PRs): main

However, the repository's remote HEAD is set to staging:

$ git remote show origin | grep "HEAD branch"
  HEAD branch: staging

Claude Code appears to detect the "main branch" by looking for a branch literally named main or master, rather than checking the remote HEAD (refs/remotes/origin/HEAD). This causes Claude to target PRs at main instead of the actual default branch.

Impact

When asked to create a pull request, Claude targets main (as instructed by its own system prompt) instead of the correct default branch (staging in our case). This requires manual correction every time and has caused PRs to be opened against the wrong branch.

The workaround is adding an explicit override in CLAUDE.md or project memory, but the auto-detection should work correctly in the first place.

Expected behavior

The "Main branch" in the system prompt should reflect the remote's HEAD branch (i.e., the output of git remote show origin | grep "HEAD branch" or the target of refs/remotes/origin/HEAD).

Environment

  • Claude Code version: 2.1.37
  • OS: macOS (Darwin 25.2.0)
  • Git remote HEAD: staging
  • Detected main branch in system prompt: main

View original on GitHub ↗

6 Comments

github-actions[bot] · 6 months ago

Found 3 possible duplicate issues:

  1. https://github.com/anthropics/claude-code/issues/19527
  2. https://github.com/anthropics/claude-code/issues/17520
  3. https://github.com/anthropics/claude-code/issues/23748

This issue will be automatically closed as a duplicate in 3 days.

  • If your issue is a duplicate, please close it and 👍 the existing issue instead
  • To prevent auto-closure, add a comment or 👎 this comment

🤖 Generated with Claude Code

golergka · 6 months ago

This is similar to #19527, but it was close due to inactivity, so this issue is NOT a duplicate of any other currently open issue

Mart-Bogdan · 6 months ago

I don't know it fhat's correct place to post it, but Claude desktop in Code mdoe aslo detects wrong branch. It picked one random really old branch.

mvillsley · 4 months ago

The fix proposed in the body (git remote show origin / refs/remotes/origin/HEAD) still has a staleness failure mode: that local ref is set at clone time and rarely updates. If a repo temporarily repoints its default branch (e.g. during a release window) and later reverts, origin/HEAD stays stuck on the old value unless a developer runs git remote set-head origin -a. The gitStatus banner then shows the wrong branch for weeks.

A stronger source of truth: query GitHub directly.

gh repo view --json defaultBranchRef --jq .defaultBranchRef.name

Immune to stale origin/HEAD, init.defaultBranch misconfiguration (see closed #31614), and the "literally named main/master" heuristic hinted at in other closed dups.

Tradeoffs: requires gh auth (already a dependency of /review, /security-review, PR creation). Fallback chain: ghorigin/HEADmain. One network call at session init, cacheable per repo per day.

Jefferson1804 · 3 months ago

As of today, May 5 2026, I am having the same issue

Grejeru · 3 months ago

+1 to solve this issue, as for me develop is default branch and would like to be treated like this.

Maybe add a mainBranch key to .claude/settings.json:

{
"mainBranch": "develop"
}