# Branches

A branch is a test copy of a project: its tables, columns, indexes, access rules, functions and triggers, and if you ask, some of its rows. Try a migration on the branch, look at what changed, then promote the branch's migrations to the project it came from. A branch is shown under its project in the console and in the project switcher.

## Make a branch

In the console open the project, then **Project → Branches**, give the branch a name (lower-case letters, digits and hyphens, such as `try-new-orders`) and choose **Make branch**. Or from a terminal:

```
rezin branch create try-new-orders -p <project id>
rezin branch create try-new-orders --from development --data --rows 500
```

What comes:

- **The structure**, always: written out by the project's own database role and built again in the branch, exactly as [`rezin migrations baseline`](https://rezincore.com/docs/tools/cli.md#starting-from-a-project-built-in-the-console) would. Each table keeps the row-level security setting it has in the project. Triggers Rezin Core puts on for [live updates](https://rezincore.com/docs/realtime.md) and [webhooks](https://rezincore.com/docs/webhooks.md) are not copied; switch them on in the branch if you need them.
- **The project's migrations**, as already applied, so the branch starts level with the project and only its own new migrations are promoted later.
- **Rows**, only with **Copy some rows too** (`--data`): up to the number per table you choose (1,000 unless you say; at most 10,000) and 50 MB in all. Tables are copied in the order their links need, and a row that points at a row that did not come is left out and counted. Counters (serial and identity columns) carry on after the copied rows. Rows that belonged to one of the project's organizations belong to the branch's own organization.
- **Accounts**, only with **Copy the application's accounts too** (`--accounts`, which needs `--data`): each organization by name, and each account by name and email address, **switched off, with no password, second step, passkey or linked provider**. Rows that pointed at an account point at its copy. An owner can switch an account on in the branch; it then signs in by resetting its password.

What never comes: secrets, keys (the branch gets new ones, shown once), sessions and sign-ins, access tokens and connections, files, functions, webhooks, email settings, and anything else outside the project's own section of the database.

A branch cannot have branches of its own.

## A branch is a project while it exists

A branch counts as one of the company's projects against the [plan's limit](https://rezincore.com/docs/plans-and-limits.md) for as long as it exists. If the company already has as many projects as its plan allows, a branch cannot be made until a project or another branch is deleted.

Its usage counts like any project's. It has its own address, keys, accounts page and record; staff cannot open it, as with any project.

## Removed after a week unused

A branch that nobody uses for **7 days** is removed: no call to its API, and nobody opening it in the console or reaching it with a token. Each company owner gets an email the day before; using the branch in any of those ways keeps it for another 7 days. Only the branch is removed, with its tables and rows. The project it came from is never touched.

The Branches page shows, for each branch, when it was last used and when it will be removed if nobody uses it.

## What differs

Open a branch on the project's Branches page (or `rezin branch diff <name>`) to see, line by line, what is only in the branch and what is only in the project: tables, columns with their types and defaults, indexes, links and checks, access rules, functions and triggers. Below it are the migrations each has that the other has not.

## Promote

Promoting applies the **branch's own migrations** to the project it came from: the ones applied to the branch (with `rezin migrations push`, `rezin env push` or the connector's `apply_migration`) that the project has not had. Changes made in the branch's table editor or SQL workspace are shown as differences but are never promoted; write them as a migration in the branch first. Rows never come back.

It is a reviewed step:

1. **Review and promote** shows every migration's text, in order, and warns if the project has had migrations of its own since the branch was made.
2. You type the project's name. What is applied is exactly what was shown: if the branch's migrations changed in between, nothing is applied and you are asked to look again.
3. Each migration runs as the project's own database role, whole or not at all, and is recorded as applied; promoting stops at the first that fails.

From a terminal, `rezin branch promote <name>` prints the same review and changes nothing; `--yes` applies exactly that.

**Production projects.** If the project is marked as [production](https://rezincore.com/docs/environments.md#production), nothing is applied straight away: each migration goes to the project's approval queue (**Connect → Assistants and approvals**), where an owner approves or refuses it. A token needs to have been allowed to reach production to promote to one.

Promotes are on the project's record as `console.branch.promote`, with who did it and what was applied or queued.

## With environments

In a folder set up with [`rezin init`](https://rezincore.com/docs/environments.md#with-the-command-line-tool), `--from <environment>` makes the branch from that environment's project, and the branch becomes one more environment, `branch-<name>`, in `rezin/config.json`:

```
rezin branch create try-it --from development
rezin env push branch-try-it               # your new migration, on the branch only
rezin env diff development branch-try-it
rezin branch promote try-it --from development --yes
rezin branch delete try-it --from development --yes
```

`rezin env list` marks it as a branch. Deleting it removes it from `rezin/config.json` too.

## Who can

| | Owners | Developers | Read-only |
|---|---|---|---|
| See branches and differences | Yes | Yes | Yes |
| Make, promote and delete branches | Yes | Yes | No |

Through a token: making a branch needs a read-write token with **Database** and **Your account and companies** that covers the whole company; promoting and deleting need a read-write token with **Database**. Connected assistants can apply migrations to a branch, but cannot make, promote or delete one.
