---
title: "Lesson 8: Workspaces and their paths"
description: "Hold only the part of the repository you work on, change it as you go with af sparse, and see where your edits go when a path leaves."
---

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

# Lesson 8: Workspaces and their paths

**Time:** 20 minutes. **Track:** [developer](https://docs.forge.workreef.dev/learn/#developer). **Before this:** [lesson 2](https://docs.forge.workreef.dev/learn/developer/changes-and-stacks/).

By the end of this lesson you will be able to:

- make a workspace that holds only some paths, and list them;
- add and remove paths with `af sparse`, as `jj sparse` does;
- say which landing a path you add comes from, and how to bring a newer one;
- say where an edit goes when its path leaves the workspace.

## A repository of three parts

A workspace holds the files of main that match its paths, every file by default. Start a forge and land a repository with two services and a library, from a workspace of every file:

```console server
$ af-server --dev
...
```

```console check
$ source ~/af-dev/dev/env
$ af workspace add ~/all
Workspace at /home/you/all
Paths: **
main has no landing yet
$ cd ~/all
$ af new -m "Lay out the services"
Working on change znunmtrrkrlqvvuuvumpnqkoyymzzmvq at commit ab4393cb6b26af53761071d9e3f161a544ff6020bf3fa5058dbb59b9e59ff12d
$ mkdir -p services/api services/web libs
$ printf 'serve the api\n' > services/api/main.txt
$ printf 'serve the pages\n' > services/web/main.txt
$ printf 'shared code\n' > libs/common.txt
$ af submit
Submitted commit b27159c0174453ee28a1273155c17a78bcd3c128a99ec27b9b9c41d6c1b2252e, 3 edits in its last snapshot
```

```console eventually
$ af status
Change znunmtrrkrlqvvuuvumpnqkoyymzzmvq (landed) at commit b27159c0174453ee28a1273155c17a78bcd3c128a99ec27b9b9c41d6c1b2252e
```

## Hold only what you work on

`--path` takes a glob from the repository's root, and repeats. A workspace for the API holds its service and the library, and nothing else is written:

```console check
$ af workspace add ~/api --path 'services/api/**' --path 'libs/**'
Workspace at /home/you/api
Paths: services/api/** libs/**
At landing 1, 2 files written
$ cd ~/api
$ af sparse list
services/api/**
libs/**
$ find . -path ./.af -prune -o -type f -print | sort
./libs/common.txt
./services/api/main.txt
```

A workspace that holds less is faster to sync and to snapshot, and keeps what you, or an agent, reads to what matters.

## Add a path

Meanwhile, the web service changes on main, from the other workspace:

```console check
$ cd ~/all
$ af new -m "Add a footer"
Working on change rsosxlkurvoukxpotzqqoprwzzvvtukw at commit e3e889a9146b8fc7f0bc418fcee1c0f10e57ce68a0da436ebc15e4b496b1d2fb
$ printf 'serve the pages, with a footer\n' > services/web/main.txt
$ af submit
Submitted commit 648eda4a7df6df8ef70bc65a40c4c9b71ec03e903b009f2209b88831309c920f, 1 edit in its last snapshot
```

```console eventually
$ af status
Change rsosxlkurvoukxpotzqqoprwzzvvtukw (landed) at commit 648eda4a7df6df8ef70bc65a40c4c9b71ec03e903b009f2209b88831309c920f
Based on landing 1
```

Back in the API's workspace, add the web service:

```console check
$ cd ~/api
$ af sparse set --add 'services/web/**'
Added 1 file, removed 0 files
$ cat services/web/main.txt
serve the pages
```

No footer: the file comes as your workspace's landing holds it, landing 1. `af sparse` changes which files you hold, never which landing they are from, as `jj sparse set` leaves `@` where it is. `af sync` brings the newer landings, to every path you hold:

```console check
$ af sync
At landing 2: 1 file written, 0 deleted
$ cat services/web/main.txt
serve the pages, with a footer
```

## Remove a path you edited

Start a change that edits both services, then take the web service away:

```console check
$ af new -m "Log each request"
Working on change xyzvqzmklvoyrzouwvotxtnltpskowrr at commit dba2e130aeda561f815cd928cd58fe23126ac231c7ec44a164516f2e573bb0cf
$ printf 'serve the api, logging each request\n' > services/api/main.txt
$ printf 'serve the pages, logging each request\n' > services/web/main.txt
$ af sparse set --remove 'services/web/**'
Added 0 files, removed 1 file
$ find . -path ./.af -prune -o -type f -print | sort
./libs/common.txt
./services/api/main.txt
```

The file left your disk, but not your change: `af sparse` snapshots your files before it changes anything, so the edit was in the change first. The change still holds it:

```console check
$ af diff
M services/api/main.txt
M services/web/main.txt
--- a/services/api/main.txt
+++ b/services/api/main.txt
@@ -1 +1 @@
-serve the api
+serve the api, logging each request
--- a/services/web/main.txt
+++ b/services/web/main.txt
@@ -1 +1 @@
-serve the pages, with a footer
+serve the pages, logging each request
```

Hold every file again with `af sparse reset`, and the edit comes back from your change:

```console check
$ af sparse reset
Added 1 file, removed 0 files
$ cat services/web/main.txt
serve the pages, logging each request
```

Two more things `af sparse` never does: it never touches a file you have not recorded, under a path you add or one you remove, and it never removes a file you edited after its snapshot; that one stays on disk, named in what `af sparse` prints.

## Exercises

1. Hold only the library: start from no paths and add `libs/**`. Your edits to both services stay in your change.

   ```console check
   $ af sparse set --clear --add 'libs/**'
   Added 0 files, removed 2 files
   $ af sparse list
   libs/**
   $ find . -path ./.af -prune -o -type f -print | sort
   ./libs/common.txt
   ```

2. Hold every file again, and check that your edit of the API came back.

   ```console check
   $ af sparse reset
   Added 2 files, removed 0 files
   $ cat services/api/main.txt
   serve the api, logging each request
   ```

## Check your understanding

<details>
<summary>You add a path, and its files are older than main's. Why, and how do you get the newest?</summary>

`af sparse` brings a path's files as the landing your workspace is at holds them, so every file you hold is from one landing, and it moves nothing else, as `jj sparse set` leaves `@` where it is. `af sync` brings the newer landings, to every path you hold.

</details>

<details>
<summary>You remove a path where you had edits. Where did they go?</summary>

Into your change: `af sparse` snapshots your files before it changes anything, so the edits are in the change, then it removes the files from disk. Add the path again, or `af sparse reset`, and they come back from the change.

</details>

<details>
<summary>An untracked file sits in a folder you add. What happens to it?</summary>

Nothing: `af sparse` never touches a file it has not recorded. It is in view now, so your next snapshot takes it into your change, as jj takes in a new file. Where main has a file at the same path, `af sparse` leaves yours and names it.

</details>

## Next

That is the developer track. The [curriculum map](https://docs.forge.workreef.dev/learn/) lists the other tracks; [Connect an agent](https://docs.forge.workreef.dev/agents/connect/) is the place to start the agent builder's. For every option of `af sparse`, see [the reference](https://docs.forge.workreef.dev/reference/af/sparse/).

Source: https://docs.forge.workreef.dev/learn/developer/workspaces-and-paths/index.mdx
