> ## Documentation Index
> Fetch the complete documentation index at: https://docs.archil.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Forks

> Fork a repo of any size in about a second: the fork is a copy-on-write copy of the repo’s disk.

`repo.fork()` creates a new repo that starts as an exact copy of the source. Nothing is copied up front: the fork's data
is a copy-on-write [branch](https://docs.archil.com/concepts/branches-and-checkpoints.md) of the source repo's disk, so a fork is ready in about a
second whatever the repo's size. Use forks to give each agent run, experiment, or user its own repo that starts from a
known state, without waiting for a clone.

```typescript TypeScript theme={null}
const fork = await repo.fork({ name: "my-app-experiment" });
fork.forkOf;    // repo.id
fork.remoteUrl; // its own remote

await fork.commit({
  branch: "main",
  message: "Try a different config",
  author: { name: "Agent", email: "agent@example.com" },
  files: [{ path: "config.json", content: '{"mode":"fast"}\n' }],
});
```

```python Python theme={null}
fork = repo.fork(name="my-app-experiment")
fork.fork_of     # repo.id
fork.remote_url  # its own remote

fork.commit(
    branch="main",
    message="Try a different config",
    author={"name": "Agent", "email": "agent@example.com"},
    files=[{"path": "config.json", "content": '{"mode":"fast"}\n'}],
)
```

The same call is `archil.git.repos.fork(repoId, { name, ref? })` when you have only the ID.

## What a fork gets

* **A consistent copy.** The fork has every branch and tag the source had at the moment of the fork, and its LFS objects.
  A push or commit that was in progress isn't half-applied in the fork.
* **Its own repo.** A fork is a repo like any other, with its own ID, name, and remote URL. It appears in
  `repos.list()`, works with the read API, and can be forked again. `forkOf` names the source.
* **Isolation.** Writes to the fork never reach the source, and writes to the source never reach the fork.
* **Separate tokens.** A token minted for the source doesn't open the fork, and the reverse. Mint tokens for the fork
  by its ID or `Repo`.
* **Its own webhooks.** The fork's starting state sends no webhook. Its own pushes and commits do, with the fork's repo ID.

### Start the fork at another ref

By default, the fork's refs are exactly the source's. Pass `ref` to point the fork's default branch at a different
branch, tag, or commit of the source. The default branch keeps the source's name.

```typescript TypeScript theme={null}
const fromRelease = await repo.fork({ name: "my-app-v1-2-hotfix", ref: "v1.2" });
// fromRelease's main is at the v1.2 tag
```

```python Python theme={null}
from_release = repo.fork(name="my-app-v1-2-hotfix", ref="v1.2")
# from_release's main is at the v1.2 tag
```

## Delete a fork

Deleting a fork is immediate: it disappears from listings, and its tokens stop working.

A repo that still has live forks can't be deleted on its own, because its forks share its disk. The call fails with
code `has_forks` (HTTP 409), and `forks` lists them, each with `id` and `name`. Delete the forks first, or pass
`cascade` to delete them with the repo:

```typescript TypeScript theme={null}
import { ArchilGitError } from "disk";

await fork.delete();

try {
  await repo.delete();
} catch (err) {
  if (err instanceof ArchilGitError && err.code === "has_forks") {
    console.log("live forks:", err.forks);
    await repo.delete({ cascade: true }); // deletes the forks, then the repo and its disk
  } else {
    throw err;
  }
}
```

```python Python theme={null}
from archil.git import ArchilGitError

fork.delete()

try:
    repo.delete()
except ArchilGitError as e:
    if e.code != "has_forks":
        raise
    print("live forks:", e.forks)
    repo.delete(cascade=True)  # deletes the forks, then the repo and its disk
```

A fork's storage is reclaimed when the original repo, whose disk the family shares, is deleted. Archil can't delete a
single branch of a disk yet.

## Notes

* A fork's `diskId` is the disk of the original repo, which the whole family shares. The fork's data is on its own
  branch of that disk.
* Forking takes a name that follows the same rules as `repos.create`, and must not already be taken.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.