claude-code - 💡(How to fix) Fix git diff uses bare branch name instead of origin/ prefix, fails when no local branch exists [1 comments, 2 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#46134Fetched 2026-04-11 06:28:11
View on GitHub
Comments
1
Participants
2
Timeline
4
Reactions
0
Timeline (top)
labeled ×3commented ×1

Error Message

  1. Observe the error:

Root Cause

In the Claude Code binary, the getDefaultBranch function reads refs/remotes/origin/HEAD and strips the refs/remotes/origin/ prefix, returning just the branch name (e.g., "develop"). The caller then passes this directly to git diff --name-only HEAD develop without prepending origin/.

Relevant logic (deминified):

async function getDefaultBranch() {
  let gitDir = await getGitDir();
  if (!gitDir) return "main";
  let commonDir = await getCommonDir(gitDir) ?? gitDir;
  // Reads refs/remotes/origin/HEAD -> returns bare name like "develop"
  let branch = await readSymRef(commonDir, "refs/remotes/origin/HEAD", "refs/remotes/origin/");
  if (branch) return branch;
  for (let name of ["main", "master"])
    if (await refExists(commonDir, `refs/remotes/origin/${name}`)) return name;
  return "main";
}

Fix Action

Workaround

Either:

  • Create a local tracking branch: git fetch origin <branch>:<branch> (only works if the branch actually exists on the remote)
  • Fix origin/HEAD if it's stale: git remote set-head origin <actual-default-branch>

Code Example

> git diff --name-only HEAD develop
fatal: ambiguous argument 'develop': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

---

async function getDefaultBranch() {
  let gitDir = await getGitDir();
  if (!gitDir) return "main";
  let commonDir = await getCommonDir(gitDir) ?? gitDir;
  // Reads refs/remotes/origin/HEAD -> returns bare name like "develop"
  let branch = await readSymRef(commonDir, "refs/remotes/origin/HEAD", "refs/remotes/origin/");
  if (branch) return branch;
  for (let name of ["main", "master"])
    if (await refExists(commonDir, `refs/remotes/origin/${name}`)) return name;
  return "main";
}
RAW_BUFFERClick to expand / collapse

Bug Description

Claude Code's internal getDefaultBranch function resolves the repo's default branch name (e.g., develop, main) by reading refs/remotes/origin/HEAD, but returns just the branch name without the origin/ prefix. This value is then used in a git diff --name-only HEAD <branch> command, which fails if no local branch with that name exists.

Steps to Reproduce

  1. Have a repo where origin/HEAD points to a branch (e.g., origin/develop) that does not exist as a local branch
  2. Run git push (or any action that triggers Claude Code's pre-push git diff)
  3. Observe the error:
> git diff --name-only HEAD develop
fatal: ambiguous argument 'develop': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

Expected Behavior

The command should use origin/develop (or origin/<branch>) instead of the bare branch name, since the branch was resolved from a remote ref and may not exist locally.

Root Cause

In the Claude Code binary, the getDefaultBranch function reads refs/remotes/origin/HEAD and strips the refs/remotes/origin/ prefix, returning just the branch name (e.g., "develop"). The caller then passes this directly to git diff --name-only HEAD develop without prepending origin/.

Relevant logic (deминified):

async function getDefaultBranch() {
  let gitDir = await getGitDir();
  if (!gitDir) return "main";
  let commonDir = await getCommonDir(gitDir) ?? gitDir;
  // Reads refs/remotes/origin/HEAD -> returns bare name like "develop"
  let branch = await readSymRef(commonDir, "refs/remotes/origin/HEAD", "refs/remotes/origin/");
  if (branch) return branch;
  for (let name of ["main", "master"])
    if (await refExists(commonDir, `refs/remotes/origin/${name}`)) return name;
  return "main";
}

Workaround

Either:

  • Create a local tracking branch: git fetch origin <branch>:<branch> (only works if the branch actually exists on the remote)
  • Fix origin/HEAD if it's stale: git remote set-head origin <actual-default-branch>

Environment

  • Claude Code version: 2.1.96 (VS Code extension, anthropic.claude-code-2.1.96-darwin-arm64)
  • OS: macOS (Darwin, arm64)

extent analysis

TL;DR

Modify the getDefaultBranch function to return the full ref path, including the origin/ prefix, to correctly resolve the default branch in the git diff command.

Guidance

  • Update the getDefaultBranch function to prepend origin/ to the branch name before returning it, ensuring the git diff command uses the correct ref path.
  • Verify the fix by checking if the git diff command successfully runs without errors after modifying the getDefaultBranch function.
  • Consider adding a check to ensure the returned branch exists locally before running the git diff command, to handle cases where the branch is not checked out locally.
  • If modifying the getDefaultBranch function is not feasible, use the provided workaround to create a local tracking branch or fix the origin/HEAD ref.

Example

async function getDefaultBranch() {
  // ...
  let branch = await readSymRef(commonDir, "refs/remotes/origin/HEAD", "refs/remotes/origin/");
  if (branch) return `origin/${branch}`; // Prepend 'origin/' to the branch name
  // ...
}

Notes

The provided workaround may not work in all cases, especially if the branch does not exist on the remote or if origin/HEAD is stale. Modifying the getDefaultBranch function to return the full ref path is a more robust solution.

Recommendation

Apply the workaround to create a local tracking branch or fix the origin/HEAD ref, as modifying the getDefaultBranch function may require updates to the Claude Code binary.

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