Main branch detection ignores remote HEAD, hardcodes 'main'
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
6 Comments
Found 3 possible duplicate issues:
This issue will be automatically closed as a duplicate in 3 days.
🤖 Generated with Claude Code
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
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.
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/HEADstays stuck on the old value unless a developer runsgit remote set-head origin -a. The gitStatus banner then shows the wrong branch for weeks.A stronger source of truth: query GitHub directly.
Immune to stale
origin/HEAD,init.defaultBranchmisconfiguration (see closed #31614), and the "literally namedmain/master" heuristic hinted at in other closed dups.Tradeoffs: requires
ghauth (already a dependency of/review,/security-review, PR creation). Fallback chain:gh→origin/HEAD→main. One network call at session init, cacheable per repo per day.As of today, May 5 2026, I am having the same issue
+1 to solve this issue, as for me develop is default branch and would like to be treated like this.
Maybe add a
mainBranchkey to.claude/settings.json:{
"mainBranch": "develop"
}