anthropics / anthropics/claude-code-action

Bug: Fork PRs fail with "couldn't find remote ref" - branch.ts doesn't use refs/pull/*/head

Open
#821 1 comment 8 reactions 0 assignees View on GitHub
bug duplicate p1
Dominant language
TypeScript
Stars
8.9k
Forks
2.1k
Avg merge
3d 9h
Merged PRs (30d)
10

Description

## Bug: Fork PRs fail with "couldn't find remote ref" - branch.ts doesn't use refs/pull/*/head

### Description

The action fails on PRs from forks with:
```
Prepare step failed with error: Command failed: git fetch origin --depth=20
fatal: couldn't find remote ref
```

This affects both `pull_request_target` and `issue_comment` triggers.

### Root Cause

In `src/github/operations/branch.ts`, the action fetches branches by name from origin:

```typescript
const branchName = prData.headRefName;
execGit(["fetch", "origin", `--depth=${fetchDepth}`, branchName]);
```

For fork PRs, the branch doesn't exist on `origin` — it exists in the fork's repo. The code should use GitHub's PR refs instead:

```typescript
// For fork PRs, use PR ref instead of branch name
const ref = isFork ? `refs/pull/${prNumber}/head` : branchName;
execGit(["fetch", "origin", `--depth=${fetchDepth}`, ref]);
```

### Related Issues

- #223 - Same error, open since July 2025
- #339 - Fork PR OIDC issues (different root cause but same symptom)
- #46 - issue_comment triggers wrong branch for forks

### Environment

- Action version: v1 (v1.0.29)
- Trigger: `issue_comment` on a PR from a fork
- Workflow correctly checks out the PR via `gh pr checkout` before the action runs, but the action's internal fetch still fails

### Expected Behavior

Fork PRs should work the same as same-repo PRs. The action should detect fork PRs and use `refs/pull/{number}/head` for fetching.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.