# Diff, merge and undo

URL: https://skywake.dev/docs/filesystem/diff-and-merge

See what a branch changed, merge it into another, settle conflicts, and undo commits.

Review what a branch changed, bring its work into another branch, and take back a change you don't want.

```bash tab="CLI"
echo "retries = 3" | skywake write app/config.toml

skywake fork app agent-1
echo "export const ttl = 3600;" | skywake write app@agent-1/src/session.ts -m "Add sessions"
echo "retries = 5" | skywake write app@agent-1/config.toml

skywake diff app@agent-1
skywake merge app@agent-1 --dry-run
skywake merge app@agent-1 -m "Sessions from agent-1"
skywake log app --limit 3
```

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

const app = new Skywake().disk("app");
await app.write({ "README.md": "# App\n", "config.toml": "retries = 3\n" });

const work = await app.fork("agent-1");
await work.write("src/session.ts", "export const ttl = 3600;\n", { message: "Add sessions" });
await work.write("config.toml", "retries = 5\n");
await app.write("config.toml", "retries = 10\n");

console.log(await work.diff("main"));

try {
  await work.mergeInto("main");
} catch (error) {
  if (!(error instanceof MergeConflict)) throw error;
  console.log("conflicts:", error.conflicts);
  const { commit } = await work.mergeInto("main", { prefer: "theirs" });
  console.log("merged as", commit);
}

console.log(await app.read("config.toml"));
```

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

app = Skywake().disk("app")
app.write({"README.md": "# App\n", "config.toml": "retries = 3\n"})

work = app.fork("agent-1")
work.write("src/session.py", "TTL = 3600\n", message="Add sessions")
work.write("config.toml", "retries = 5\n")
app.write("config.toml", "retries = 10\n")

print(work.diff("main"))

try:
    work.merge_into("main")
except MergeConflict as error:
    print("conflicts:", error.conflicts)
    merged = work.merge_into("main", prefer="theirs")
    print("merged as", merged["commit"])

print(app.read("config.toml"))
```

## Diff [#diff]

`work.diff("main")` (CLI: [`skywake diff app@agent-1 [main]`](/reference/cli/diff)) lists the paths that differ between the branch and another branch or commit, each with its `change`: `added`, `removed` or `modified`. The CLI marks them `+`, `-` and `~`. A folder that is new on one side shows up once, not file by file. Folders that didn't change are skipped without being read, so a diff costs as much as what changed, whatever the size of the tree.

The comparison is with the other branch as it is now, so changes made on `main` since the fork show up too.

## Merge [#merge]

`work.mergeInto("main")` (CLI: [`skywake merge app@agent-1`](/reference/cli/merge), `--into` for another branch) brings the branch's changes into another. It is a three-way merge against the newest commit both branches share.

- When both sides changed a text file in different places, the changes are merged line by line.
- A file's mode is merged on its own, apart from its content.
- When changes overlap, nothing is saved and the merge fails with `MergeConflict`. Its `conflicts` lists the paths. The CLI prints `conflict: PATH` for each and exits non-zero.

| Option    | CLI         | Meaning                                                                                    |
| --------- | ----------- | ------------------------------------------------------------------------------------------ |
| `message` | `-m`        | The merge commit's message.                                                                |
| `dryRun`  | `--dry-run` | Only report the conflicts the merge would have now. Saves nothing.                         |
| `prefer`  | —           | Settle every conflict by keeping `into`'s side (`ours`) or the merged branch's (`theirs`). |

The result is `{ commit, conflicts }`. Merges into one branch take turns, like any other write.

## Settle conflicts yourself [#settle-conflicts-yourself]

To combine both sides instead of picking one, merge the other way first, then write the result on the fork:

1. Try the merge. On `MergeConflict`, read each conflicting path on both branches and decide what it should hold.
2. Merge `main` into the fork with `prefer: "ours"`. The fork now includes `main`, keeping its own side of each conflict.
3. Write the settled files on the fork, and merge it into `main` again. This time nothing overlaps.

```ts
import { MergeConflict, Skywake } from "skywake";

const sky = new Skywake();
const app = sky.disk("app");
await app.write("TODO.md", "- ship login\n");

const work = await app.fork("agent-1");
await work.write("TODO.md", "- ship login\n- add sessions\n");
await app.write("TODO.md", "- ship login\n- fix signup\n");

const combine = (ours: string, theirs: string) =>
  [...new Set([...ours.split("\n"), ...theirs.split("\n")])].filter(Boolean).join("\n") + "\n";

try {
  await work.mergeInto("main");
} catch (error) {
  if (!(error instanceof MergeConflict)) throw error;
  const settled: Record<string, string> = {};
  for (const path of error.conflicts) {
    settled[path] = combine(await app.read(path), await work.read(path));
  }
  await app.mergeInto(work.branch, { prefer: "ours" });
  await work.write(settled, { message: "Settle conflicts with main" });
  await work.mergeInto("main");
}

console.log(await app.read("TODO.md"));
```

`combine` is your own code, such as an agent that reads both versions and writes one.

Per-path choices in a single merge request (`resolve`, with `take`, `text` or `delete` for each path) exist only on the v1 API, [`POST /v1/namespaces/{namespace}/merges`](/reference/api/history/mergeBranches). `/v1` goes away one release after v2.

## Undo [#undo]

```ts
import { Skywake } from "skywake";

const app = new Skywake().disk("app");
const good = await app.write("config.toml", "retries = 3\n", { message: "Set retries" });
const bad = await app.write("config.toml", "retries = 0\n", { message: "Turn retries off" });

console.log("then:", await app.read("config.toml", { at: good.commit }));

await app.revert(bad.commit);
console.log("after revert:", await app.read("config.toml"));

await app.write("notes.md", "draft\n");
await app.restore(good.commit);
console.log("after restore:", await app.exists("notes.md"));

for await (const commit of app.log({ limit: 5 })) {
  console.log(commit.id.slice(0, 12), commit.message);
}
```

- `app.revert(commit)` undoes one commit by adding a new commit that puts back what it changed. For a merge commit, it goes back to the first parent. If later commits changed the same paths again, nothing is saved and it fails with `MergeConflict`.
- `app.restore(commitOrTime)` makes the branch look like it did then, as one new commit on top. The commit can come from any branch of the disk. In TypeScript a `Date` picks the newest commit at or before that time.
- `read`, `list`, `grep`, `find` and `download` take `at` to look at the past without changing anything.

Both return `{ commit, changed }`. `changed` is `false` when there was nothing to undo. Both need a `write` key. A [protected branch](./protection.mdx) refuses both, because it changes only through a merge. There is no CLI command for revert or restore yet; use an SDK or `POST /v2/disks/{disk}/revert` and `/restore`.

## History [#history]

`app.log({ path, limit })` lists commits newest first: each has an `id`, a `message`, when it was `created`, and for a merge, the commit it `merged`. Pass `path` to see only the commits that changed one file. In the CLI it is [`skywake log app@agent-1 --path src/app.py --limit 10`](/reference/cli/log).

## Next [#next]

<Cards>
  <Card href="./protection.mdx" />
  <Card href="../agents/cli.mdx" />
  <Card href="/reference/cli/log" />
</Cards>