gh repo fork creates a fork of a repository under your account and can clone it locally.
Contribution bots fork upstream, branch, and open a PR; gh repo fork handles the fork and remote wiring in one step.
What it does
gh repo fork forks the named repository (or the current one) into your account or an organization. With --clone it clones the fork, and --remote configures an upstream remote pointing at the original repository.
Set GH_TOKEN. The default GITHUB_TOKEN cannot fork to a personal account or another org, so forking in Actions requires a PAT or App token with repo and the right org scope. Forking is asynchronous; the fork may take a few seconds to be ready to clone.
Common errors in CI
"Resource not accessible by integration" means GITHUB_TOKEN was used where forking needs a PAT. "HTTP 403" can mean forking is disabled for the org. If --clone runs before the fork is fully created, the clone may briefly fail; gh retries, but a custom script should add a short wait.
Using this in CI
CI checkouts are shallow and detached by default, which changes the answer this command gives you. Commands that read history, branch names, or tags need the checkout configured for it.
.github/workflows/ci.yml
- uses:actions/checkout@v4with:fetch-depth:0 # history, tags, and git describe all need this- run:|git rev-parse --is-shallow-repository # expect falsegit rev-parse --abbrev-ref HEAD # prints HEAD when detached
Frequently asked questions
gh repo fork: Fork a Repository in CI?
Contribution bots fork upstream, branch, and open a PR; gh repo fork handles the fork and remote wiring in one step.
What it does?
gh repo fork forks the named repository (or the current one) into your account or an organization. With --clone it clones the fork, and --remote configures an upstream remote pointing at the original repository.
In CI?
Set GH_TOKEN. The default GITHUB_TOKEN cannot fork to a personal account or another org, so forking in Actions requires a PAT or App token with repo and the right org scope. Forking is asynchronous; the fork may take a few seconds to be ready to clone.
Common errors in CI?
"Resource not accessible by integration" means GITHUB_TOKEN was used where forking needs a PAT. "HTTP 403" can mean forking is disabled for the org. If --clone runs before the fork is fully created, the clone may briefly fail; gh retries, but a custom script should add a short wait.