# Branches and forks

URL: https://skywake.dev/docs/filesystem/branches

Every disk starts with a main branch. A fork is an instant copy that shares the data.

A branch is a line of commits on a disk. Every disk starts with `main`. Fork a branch when you want to change it without touching the original, for example to give an agent its own copy of `main`.

```ts tab="TypeScript"
import { Skywake } from "skywake";

const sky = new Skywake();
const app = sky.disk("app");
await app.write("src/auth.ts", "export const timeout = 30;\n");

const work = await app.fork("agent-1");
await work.write("src/auth.ts", "export const timeout = 60;\n");

console.log("main:   ", await app.read("src/auth.ts"));
console.log("agent-1:", await work.read("src/auth.ts"));
console.log((await app.branches()).map((branch) => branch.name));

await work.destroy();
```

```py tab="Python"
from skywake import Skywake

sky = Skywake()
app = sky.disk("app")
app.write("src/auth.py", "TIMEOUT = 30\n")

work = app.fork("agent-1")
work.write("src/auth.py", "TIMEOUT = 60\n")

print("main:   ", app.read("src/auth.py"))
print("agent-1:", work.read("src/auth.py"))
print([branch["name"] for branch in app.branches()])

work.destroy()
```

```bash tab="CLI"
echo "TIMEOUT = 30" | skywake write app/src/auth.py

skywake fork app agent-1
echo "TIMEOUT = 60" | skywake write app@agent-1/src/auth.py

skywake read app/src/auth.py
skywake read app@agent-1/src/auth.py
skywake branches app

skywake destroy app@agent-1 --yes
```

## Forks [#forks]

`fork(name)` makes a new branch from the one the handle is on and returns a handle to it. In the CLI, `skywake fork app agent-1` forks `main`, and `--from` picks another branch. To use a branch that already exists, ask for it by name: `sky.disk("app", { branch: "agent-1" })` in TypeScript, `sky.disk("app", branch="agent-1")` in Python, or `app@agent-1` in the CLI.

A fork copies a pointer, never the data, so it takes the same time however much the branch holds. From then on each branch sees only its own commits, and the files both share are stored once.

To fork an older commit instead of the newest, pass `at`: `app.fork("audit", { at: commit })` in TypeScript or `app.fork("audit", at=commit)` in Python.

## Listing branches [#listing-branches]

`app.branches()` (CLI: [`skywake branches app`](/reference/cli/branches)) lists every branch with its `name`, the branch it was forked `from`, and when it was `created`. `work.head()` returns a branch's newest commit.

## Undo [#undo]

You can undo work on a branch in two ways. [Revert](./diff-and-merge.mdx#undo) undoes one commit. [Restore](./diff-and-merge.mdx#undo) makes the branch look like it did at an earlier commit. Both add a new commit on top, so the history is kept.

## Two writers on one branch [#two-writers-on-one-branch]

Moving a branch's head is a conditional write. When two writers start from the same head at the same time, one wins and the other gets a `Conflict` error. To avoid this, give each agent its own fork. Then they never wait for each other.

## Destroying a branch [#destroying-a-branch]

`work.destroy()` (CLI: [`skywake destroy app@agent-1`](/reference/cli/destroy)) deletes the branch. `destroy` on `main` deletes the whole disk. The CLI asks first; pass `--yes` to skip the question.

A destroyed branch's data stays in the bucket until garbage collection removes it. On a store you run yourself, run [`gc`](/reference/cli/gc) with `--local`:

```bash norun
skywake gc --local=s3://my-bucket --dry-run
skywake gc --local=s3://my-bucket
```

| Flag              | Default          | Meaning                                                                                                                         |
| ----------------- | ---------------- | ------------------------------------------------------------------------------------------------------------------------------- |
| `--grace-minutes` | `11520` (8 days) | Only data older than this is deleted, so saves that are still in progress are safe. Under 2 days is unsafe while anyone writes. |
| `--dry-run`       | off              | Only count. Delete nothing.                                                                                                     |

`gc` needs delete rights on the whole store.

## With no server [#with-no-server]

`--local[=DIR]` (or `SKYWAKE_STORE`) runs `fork`, `merge`, `branches`, `log`, `diff`, `destroy NAME@BRANCH`, `upload` and `download` against a bucket or folder directly, with no API. The store is one disk, so only the branch in `NAME@BRANCH` is used. When `skywaked` is running and no store is named, these commands go through the daemon and act on its store, as far as its access file allows. See [mount](./mount.mdx).

## Next [#next]

<Cards>
  <Card href="./diff-and-merge.mdx" />
  <Card href="./mount.mdx" />
  <Card href="/reference/cli/fork" />
</Cards>