git diff uses bare branch name instead of origin/ prefix, fails when no local branch exists
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
- Have a repo where
origin/HEADpoints to a branch (e.g.,origin/develop) that does not exist as a local branch - Run
git push(or any action that triggers Claude Code's pre-push git diff) - 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/HEADif 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)
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗