Skip to content

Select branches

Each workspace takes the code of each repository from a source. A source is a branch, or a checkout that is on your machine. You select the sources when you make the workspace. You can change them with lo set.

  1. List the branches that the repositories of the template have:

    Terminal window
    lo template branches shop

    The table shows the services of each repository, the branch that lo new takes, and all the branches.

  2. Make the workspace. Add one service=branch pair for each source that is not the default:

    Terminal window
    lo new shop-b --template shop storefront=feature/new-checkout

    The services that you do not name take the default ref of the template.

  3. Start the workspace:

    Terminal window
    lo up shop-b

lo new fetches each repository that has an origin remote. If the fetch fails, lo new stops. It does not use a branch list that can be out of date.

Each pair has the form name=value. The name is a service or a repository of the template. The value is a branch or a path.

Pair Meaning
storefront=feature/x The branch feature/x of the repository of the service storefront.
orders-api=feature/x The branch feature/x of the repository orders-api. The name is a key in repos:.
storefront=~/code/storefront The checkout in that directory, as it is.
orders-api=remote:staging The remote staging of the service. The service does not run on your machine.
orders-api=https://... A URL that you enter as the remote of the service.

A value that starts with /, ~/, ./ or ../ is a path. A value that starts with remote:, or that has a scheme such as https://, is a remote. Any other value is a branch. See remotes.

All the services of one repository run from the same source. If two pairs give different values for one repository, lo new fails. A service with an image has no source, and you cannot select a branch for it.

Use --match to select a branch of each repository with one word. For a ticket, this is shorter than a pair for each repository.

Terminal window
lo new shop-c --template shop --match TICKET-12

For each repository that you did not name, lo new looks for the branches that contain the word. The word must be a complete part of the branch name. A part ends at the start or end of the name, or at /, -, _ or .. The match ignores case.

  • If the repository has one such branch, lo new takes it.
  • If the repository has more than one such branch, lo new fails and lists them. Name the branch that you want in a pair.
  • If the repository has no such branch, the repository takes the default ref.

localspace looks at the local branches first. It looks at the branches of origin only if no local branch matches. To see the result before you make a workspace, run lo template branches shop --match TICKET-12.

A repository that has no pair and no match takes its default ref: the ref of the repository in repos:, or defaults.ref of the template. localspace reads it from origin, where it is always current. A repository with no origin remote uses the local branch. If a repository has no pair and the template gives it no default ref, lo new fails.

A pair can also tell that a service or a resource runs elsewhere, on a deployment that localspace does not manage. The value is remote:<name> for a remote that the template lists, or a URL. A resource also takes local.

Terminal window
lo new shop-b --template shop orders-api=remote:staging
lo new shop-c --template shop orders-api=https://orders.qa.acme.com \
--attr orders-api.api_key='${secret:orders/qa_key}'

--attr <unit>.<attribute>=<value> gives an attribute to the remote of one workspace. It replaces the value of the template. lo set takes the same pairs and the same flag. See use services that run elsewhere.

A pair for a repository does not change a service that runs on a remote. A pair for the service does: a branch moves it to your machine.

lo new only records the sources. lo up makes the checkouts.

  • A branch that no worktree has: localspace makes a git worktree in worktrees/<workspace>/<repository> in the localspace home. This is an owned worktree. If the branch is only on origin, the worktree has a new local branch that tracks it.
  • The default ref: the owned worktree has a detached HEAD at the commit of origin. Each lo up moves it to the new commit of origin, if it holds no unsaved work.
  • A branch that is checked out in a worktree of your repository: localspace uses that worktree as it is. It is not an owned worktree.
  • A path: localspace uses that checkout as it is, with its current branch. If HEAD is detached, it uses HEAD.

localspace removes only the worktrees that it made. It does not change or remove a checkout that you made. See Safety.

Use lo set to move a workspace to other sources. The resources keep their data.

  1. Run the command with the new pairs. The forms are the same as for lo new:

    Terminal window
    lo set shop-b storefront=main orders-api=feature/refund
  2. Read the warnings. If the changed files include a migration, lo set tells you that lo db reset can be necessary. A migration is a .sql file, or a file in a directory with the name migrations or migrate.

  3. If necessary, run lo db reset <workspace> <resource>. See Resources.

lo set does these steps:

  1. It fetches and finds the new sources.
  2. It compares the previous commit and the new commit of each repository.
  3. It finds how each service of the repository runs from the new source.
  4. It stops each running service that must restart, and removes the previous owned worktree.
  5. It makes the new worktree, and starts the services again.

The template tells which services restart. Set on_switch on a service to restart, rebuild or none. The default is restart. A service rebuilds when a changed file matches one of its rebuild_on patterns. A pattern that ends in / matches a directory.

Only a service that runs restarts. A stopped service uses the new source at the next lo up.

lo set refuses to remove an owned worktree that holds work that is not saved. That is an uncommitted change, an untracked file, or a commit that is on no remote. The error tells you each item.

  1. Go to the worktree that the error names.
  2. Commit and push your work.
  3. Run lo set again.
  • The workspace runs from a worktree of a repository. That is, you started it with lo up in that worktree. Check out the other branch in the worktree.
  • No service of the repository runs on your machine in this workspace. The services are shared, or they run on a remote. Name a service of the repository to move it to your machine, or make a new workspace.
  • You give no pair and no --attr. lo set must have at least one.